• 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

    • 1. DRM is the worst plague, I like my games clean. 2. Third party launchers are an overhead and annoyance.
    • Amazon lays off more staff across Goodreads and Kindle divisions by Hamid Ganji Dozens of Amazon employees working on the retailer's book divisions have been laid off. As reported by Reuters, Amazon confirmed that it's cutting jobs across the Goodreads review site and Kindle units, which impacts fewer than 100 workers. Amazon says the recent layoffs across Goodreads and Kindle divisions are meant to improve efficiency and streamline operations. The giant retailer has constantly reduced staff across various divisions over the past few years. According to CEO Andy Jassy, reducing headcounts helps the company to eliminate bureaucracy. "As part of our ongoing work to make our teams and programs operate more efficiently and to better align with our business roadmap, we've made the difficult decision to eliminate a small number of roles within the Books organization," an Amazon spokesperson said. Layoffs recently impacted employees in Amazon's Wondery podcast division, devices and services units, communications, and in-store staff. However, Amazon's Q1 results show the retailer has added about 4,000 jobs compared to Q4 2024. After the Covid pandemic settled down, many companies began laying off thousands of staff they hired during the pandemic to respond to growing demands. The layoff trend among tech firms still exists today, and AI has amplified it. The latest data shows that in 2025, about 62,832 tech employees were laid off across 141 tech companies. Also, 152,922 tech employees across 551 companies were laid off in 2024. More layoffs are expected to occur due to declining economic growth, tariffs, and the expansion of AI across companies. Amazon is also gearing up to double down in AI investments and robotics. The company has recently announced the forming of a new agentic AI team to develop an agentic AI framework for use in robotics. Also, a new report by The Information indicates that Amazon has begun testing humanoid robots for package delivery.
    • Major Privacy 0.98.1.1 Beta by Razvan Serea MajorPrivacy is a cutting-edge privacy and security tool for Windows, offering unparalleled control over process behavior, file access, and network communication. It is a continuation of the PrivateWin10 project. By leveraging advanced kernel-level protections, MajorPrivacy creates a secure environment where user data and system integrity are fully safeguarded. Unlike traditional tools, MajorPrivacy introduces innovative protection methods that ensure mounted encrypted volumes are only accessible by authorized applications, making it the first and only encryption solution of its kind. MajorPrivacy – Ultimate Privacy & Security for Windows key features Process Protection – Isolate processes to block interference from unauthorized apps, even with admin privileges. Software Restriction – Block unwanted apps and DLLs to ensure only trusted software runs. Revolutionary Encrypted Volumes Secure Storage – Create encrypted disk images for sensitive data. Exclusive Access – Unlike traditional tools, only authorized apps can access mounted volumes—blocking all unauthorized processes. File & Folder Protection – Lock down sensitive files and prevent unauthorized access or modifications. Advanced Network Firewall – Control which apps can send or receive data online. DNS Monitoring & Filtering – Track domain access and block unwanted sites (Pi-hole compatible filtering coming soon). Tweak Engine – Disable telemetry, cloud integration, and invasive Windows features for better privacy. Why MajorPrivacy? Kernel-Level Security – Protects at the deepest system level. Unmatched Encryption Protection – Keeps mounted volumes safe from all unauthorized access. Full System Control – Block, isolate, or restrict processes as needed. Enhanced Privacy – Stops Windows & apps from collecting unnecessary data. Perfect for privacy-conscious users, IT pros, and anyone who wants total system control. Major Privacy 0.98.1.1 Beta changelog: The 0.98.1 release of MajorPrivacy introduces significant enhancements and a number of critical fixes aimed at improving usability, localization, and system integration. A major new feature is the introduction of full translation support, allowing the application interface and tweaks to be localized into multiple languages. Initial translations include AI-assisted German and Polish versions, a community-contributed Turkish translation, and Simplified Chinese. Users interested in contributing translations or adding new languages are encouraged to participate via the forum. This version also improves compatibility and deployment by bundling the Microsoft Visual C++ Redistributable with the installer, which is required for the ImDisk user interface. Several important bugs have been resolved. The installer now correctly removes the driver during uninstallation. Tweak definitions have been cleaned up for better consistency. A number of networking issues were addressed, including failures related to network shares and incorrect handling of mapped drive letters. It is now required to use full UNC paths for defining rules involving shared resources. Additionally, configuration persistence issues on system shutdown have been fixed, as well as problems affecting protected folder visibility and rule precedence involving enclave conditions. Finally, the underlying driver code has been refactored, laying the groundwork for better maintainability and future enhancements. MajorPrivacy-v0.98.1.1.exe (0.98.1a) hotfix for #71 Download: Major Privacy 0.98.1.1 Beta | 47.4 MB (Open Source) View: MajorPrivacy Home Page | Github Project page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
    • Week One Done
      jfam earned a badge
      Week One Done
    • First Post
      survivor303 earned a badge
      First Post
    • Week One Done
      CHUNWEI earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      430
    2. 2
      +FloatingFatMan
      239
    3. 3
      snowy owl
      212
    4. 4
      ATLien_0
      211
    5. 5
      Xenon
      157
  • Tell a friend

    Love Neowin? Tell a friend!