• 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.