• 0

[C#] linq exception handling in a query?


Question

If you get an exception while trying to make a nested query a dictionary that states a same key has already been added is there a way of actually seeing the key that is attempted to be added? In the debugger it just says the error of an key already exists with a stack trace of (I took out the actual path names):

   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at a.MainWindow.<>c__DisplayClass4a.<refreshClientLocks>b__14(DataRow acc_dr) in a\MainWindow.xaml.cs:line 301
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at a.MainWindow.<refreshClientLocks>b__12(<>f__AnonymousType0`2 <>h__TransparentIdentifier5) in a\MainWindow.xaml.cs:line 299
   at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
   at System.Linq.Enumerable.<>c__DisplayClass12`3.<CombineSelectors>b__11(TSource x)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

9 answers to this question

Recommended Posts

  • 0

yes, the inner exception is null, which makes sense b/c it's throwing the exception since a key is being added to a dictionary that already has that key definition. Since it is in a query I can't see which (to my knowledge at least)

key is attempting to be added twice.

  • 0

Not sure how to break this into smaller queries without compromising performance at a terrible rate:

var clientQuery = from DataRow client_dr in this.gl_aeiMgr.ClientsDataSet.Tables[this.gl_aeiMgr.ClientsTbl].Rows
                                                  let clientId = client_dr.Field<string>("userAccessId                                                let accList = (from DataRow acc_dr in this.gl_aeiMgr.ClientsAccDataSet.Tables[this.gl_aeiMgr.ClientsAccTbl].Rows
                                                                 where acc_dr.Field<string>("clientID clientId
                                                                 select new
                                                                 {
                                                                     AccId = acc_dr.Field<string>("userAccessId                                                                    AccDefObj = new AccDef(acc_dr.Field<string>("userAccessIdlientId, acc_dr.Field<int>("acc_numAccType)Enum.Parse(typeof(AccType), acc_dr.Field<string>("acc_type                                                                         (AccBillCycle)Enum.Parse(typeof(AccBillCycle), acc_dr.Field<string>("accScheduleType                                                                         (from DataRow benf_dr in this.gl_aeiMgr.ClientsBenfDataDet.Tables[this.gl_aeiMgr.ClientsBenfTbl].Rows
                                                                          let benfAccId = benf_dr.Field<string>("userAccessId                                                                        where benf_dr.Field<string>("benfAccId acc_dr.Field<string>("userAccessId                                                                        select new BenfDef(benf_dr.Field<string>("userAccessIdenfAccId, benf_dr.Field<string>("benfFirstNamebbenf_dr.Field<string>("benfLastNamebbenf_dr.Field<string>("benfMiddleInit                                                                             benf_dr.Field<string>("benfSSNbbenf_dr.Field<DateTime>("benfDOBbbenf_dr.Field<double>("benfPercentbbenf_dr.Field<string>("benfAddrStreet                                                                             benf_dr.Field<string>("benfAddrCityGlobals.USPSStateCodes)Enum.Parse(typeof(Globals.USPSStateCodes), benf_dr.Field<string>("benfAddrState                                                                              benf_dr.Field<string>("benfAddrZipCodebbenf_dr.Field<string>("benfAddrCountrybbenf_dr.Field<string>("benfPPhonePhoneContactType)Enum.Parse(typeof(PhoneContactType), benf_dr.Field<string>("benfPPhoneType")))).ToDictionary(oenfId, o =&gt; o),
                                                                         (from DataRow benf_dr in this.gl_aeiMgr.ClientsSBenfDataSet.Tables[this.gl_aeiMgr.ClientsSBenfTbl].Rows
                                                                          let benfAccId = benf_dr.Field<string>("userAccessId                                                                        where benf_dr.Field<string>("benfAccId acc_dr.Field<string>("userAccessId                                                                        select new BenfDef(benf_dr.Field<string>("userAccessIdenfAccId, benf_dr.Field<string>("benfFirstNamebbenf_dr.Field<string>("benfLastNamebbenf_dr.Field<string>("benfMiddleInit                                                                             benf_dr.Field<string>("benfSSNbbenf_dr.Field<DateTime>("benfDOBbbenf_dr.Field<double>("benfPercentbbenf_dr.Field<string>("benfAddrStreet                                                                             benf_dr.Field<string>("benfAddrCityGlobals.USPSStateCodes)Enum.Parse(typeof(Globals.USPSStateCodes), benf_dr.Field<string>("benfAddrState                                                                              benf_dr.Field<string>("benfAddrZipCodebbenf_dr.Field<string>("benfAddrCountrybbenf_dr.Field<string>("benfPPhonePhoneContactType)Enum.Parse(typeof(PhoneContactType), benf_dr.Field<string>("benfPPhoneType")))).ToDictionary(oenfId, o =&gt; o),
                                                                         HelperUtils.ToObservableCollection&lt;PmtDef&gt;((from DataRow pmt_dr in this.gl_aeiMgr.ClientsPmtDSet.Tables[this.gl_aeiMgr.ClientsPmtTbl].Rows
                                                                                                                     where pmt_dr.Field<string>("accId acc_dr.Field<string>("userAccessIdmp;&amp; pmt_dr.Field<bool>("pmtIsExp false
                                                                                                                     select new PmtDef(pmt_dr.Field<string>("userAccessIdppmt_dr.Field<DateTime>("pmtEffDateppmt_dr.Field<double>("pmtAmt                                                                                                                    false, pmt_dr.Field<bool>("pmtIsStmtppmt_dr.Field<DateTime>("pmtExpDateppmt_dr.Field<string>("pmtChkNum"))).AsEnumerable<PmtDef>, null, null,
                                                                                                                     (from DataRow certList_dr in this.gl_aeiMgr.ClientsCertListDSet.Tables[this.gl_aeiMgr.ClientsCertLstTbl].Rows
                                                                                                                      where certList_dr.Field<string>("accID acc_dr.Field<string>("userAccessId                                                                                                                    select new CertDef(certList_dr.Field<string>("userAccessIdccertList_dr.Field<int>("certNumccertList_dr.Field<double>("certAmt"))).GroupBy(oertAmt).ToDictionary(o =&gt; o.Key, o =&gt; o.ToList&lt;CertDef&gt;()),
                                                                                                                          (from DataRow note_dr in this.gl_aeiMgr.ClientsNotesDSet.Tables[this.gl_aeiMgr.ClientsNotesTbl].Rows
                                                                                                                           where note_dr.Field<string>("accId acc_dr.Field<string>("userAccessId                                                                                                                         select new NoteDef(note_dr.Field<string>("noteNumnnote_dr.Field<string>("noteNumnnote_dr.Field<DateTime>("noteDateEff                                                                                                                              note_dr.Field<DateTime>("noteDateExpAccBillCycle)Enum.Parse(typeof(AccBillCycle), note_dr.Field<string>("noteBillCyclerue), note_dr.Field<bool>("noteIsPaidnnote_dr.Field<double>("notePmt"))).ToDictionary(ooteId, o =&gt; o),
                                                                                                                                   (from DataRow rel_dr in this.gl_aeiMgr.ClientsRelConDSet.Tables[this.gl_aeiMgr.ClientsRelConTbl].Rows
                                                                                                                                    where rel_dr.Field<string>("accId acc_dr.Field<string>("userAccessId                                                                                                                                  select new RelContactDef(rel_dr.Field<string>("userAccessIdrrel_dr.Field<string>("fNamerrel_dr.Field<string>("lNamerrel_dr.Field<string>("addrStreetrrel_dr.Field<string>("addrCity                                                                                                                                       (Globals.USPSStateCodes)Enum.Parse(typeof(Globals.USPSStateCodes), rel_dr.Field<string>("addrStaterue), rel_dr.Field<string>("addrZipCode                                                                                                                                       rel_dr.Field<string>("addrCountryrrel_dr.Field<string>("phonerrel_dr.Field<string>("companyrrel_dr.Field<string>("email"))).ToDictionary(oelContactId, o =&gt; o),
                                                                                                                                            (from DataRow frm_dr in this.gl_aeiMgr.ClientsAccFormsDset.Tables[this.gl_aeiMgr.ClientsAccFormsTbl].Rows
                                                                                                                                             where frm_dr.Field<string>("clientsAccID acc_dr.Field<string>("userAccessId                                                                                                                                           select new FrmDetailLstDef(frm_dr.Field<string>("userAccessIdffrm_dr.Field<string>("formNameFrmStatus)Enum.Parse(typeof(FrmStatus), frm_dr.Field<string>("formStatusrue), frm_dr.Field<DateTime>("formDateCreatedffrm_dr.Field<string>("formLocation"))).ToDictionary(ormId, o =&gt; o))
                                                                 }).ToDictionary(o =&gt; o.AccId, o =&gt; o.AccDefObj)
                                                  select new ClientDef(clientId, client_dr.Field<string>("fNamecclient_dr.Field<string>("middleInitcclient_dr.Field<string>("lName                                                     client_dr.Field<string>("ssncclient_dr.Field<string>("pPhone1Numclient_dr.Field<string>("pPhone1Ext null) ? client_dr.Field<string>("pPhone1Ext"", (client_dr.Field<string>("pPhone2Num null) ? client_dr.Field<string>("pPhone2Num"",
                                                      (client_dr.Field<string>("pPhone2Ext null) ? client_dr.Field<string>("pPhone2Ext"", ClientMgr.ConvertPhoneTypeFromStr(client_dr.Field<string>("pPhone1Type                                                      ClientMgr.ConvertPhoneTypeFromStr(client_dr.Field<string>("pPhone2Typeclient_dr.Field<string>("mAddrStreetlientMgr.ConvertStateCodeFromStr(client_dr.Field<string>("mAddrState                                                      client_dr.Field<string>("mAddrZipCodecclient_dr.Field<string>("mAddrCitycclient_dr.Field<string>("mAddrCountryccList, client_dr.Field<string>("mFaxContactcclient_dr.Field<string>("pEmailAddrcclient_dr.Field<DateTime>("clientDOB                                                     client_dr.Field<int>("ppmNumcclient_dr.Field<double>("ppmInvestNum

  • 0

the bug was not within the query, it was another piece of code that was pointing to the wrong table. I didn't catch it b/c the only difference between the two datasets/table names is one has an S in the middle of it and one does not. So it was grabbing the information out of the wrong table. Thanks for the help guys!

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Adobe releases all-new Photoshop app for Android devices by Pradeep Viswanathan Early this year, Adobe released an all-new Photoshop app for iOS devices. Designed from the ground up for smartphones, the app allows users to easily add, remove, adjust, and combine content, as well as access free Adobe Stock assets to create new visuals. Today, Adobe announced a similar, brand-new Photoshop app for Android devices, currently in beta. It’s important to note that this Android version is not intended to replace the desktop version of Photoshop. Instead, it offers access to select powerful Photoshop features—including layering, masking, and the new Generative Fill—within an easy-to-use mobile interface. During the beta phase, the following features are available to all users: Following the beta phase, users will need a new Photoshop Mobile & Web plan to access the premium features. The premium features list includes the ability to remove objects by brushing over them with the Remove Tool, the ability to use Clone Stamp to hide unwanted objects by cloning areas of an image, the ability to fill portions of an image with content sampled from other parts of the image with Content-Aware Fill, the ability to export using additional file formats (PSD, TIF, JPG, PNG), and more. You can download the Adobe Photoshop app from the Google Play Store if your device is running Android 11 or later and has at least 6GB of RAM (8GB or more is recommended for optimal performance).
    • And what about all the toxic waste that "clean" nuclear energy produces?
    • Plasma is beautiful, but my workload is unlikely to ever run on linux ... Cubase 14 with 5 or 6 dozen instances of Kontakt 8. Sigh.
    • Hey everyone! Just curious what your favorite browser is and why? Personally, I prefer using Chrome because it is simple and smooth to use. Would love to hear what everyone else is using and if there's something better I should try!
    • I'm Sam, 20, currently studying IT and passionate about all things tech and music. I found Neowin while browsing for community forums, and I’m excited to be here, connect with others, and maybe even learn something new along the way. Looking forward to chatting with you all!
  • Recent Achievements

    • Conversation Starter
      DarkShrunken earned a badge
      Conversation Starter
    • One Month Later
      jrromero17 earned a badge
      One Month Later
    • Week One Done
      jrromero17 earned a badge
      Week One Done
    • Conversation Starter
      johnwin1 earned a badge
      Conversation Starter
    • One Month Later
      Marwin earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      246
    2. 2
      snowy owl
      156
    3. 3
      ATLien_0
      142
    4. 4
      +FloatingFatMan
      138
    5. 5
      Xenon
      127
  • Tell a friend

    Love Neowin? Tell a friend!