Solved

Questions Tags Users Badges Unanswered Ask Question

Posted on 2011-09-15
1
45,403 Views
Last Modified: 2012-05-12
I have the following datatable, which has Units information. The Units have a surface in square meters. However the units can be split into subunits, in the datatable there is a ParentUnitID to know if that unit is splitted or not.

I need to validate that the sum of the surface of the child units, is equal to the surface of the parent unit.

 bWJwN.png

In anoter forum they helped me with this code


 var childrenByParentID = _unitDataSet.Unit.AsEnumerable().
                                     GroupBy(child => child.Field<int?>("ParentUnitId")).
                                     ToDictionary(group => group.Key);

            // Parents = children with no parent
            var parents = childrenByParentID[null];

            // Look for the first group which sum of surfaces is not the same as the parent's surface
            var invalidGroup = childrenByParentID.FirstOrDefault(group =>
            {
                bool invalid = false;

                if (group.Key != null)
                {
                    // Not the parents group
                    var parent = parents.Single(parent => parent.Field<int>("UnitID") == group.Key);

                    var totalSurface = group.Sum(group => group.Field<int>("Surface"));
                    invalid = (totalSurface != parent.Field<int>("Surface"));
                }

                return invalid;
            });

            if (invalidGroup != null)
            {
                // .. do something special
            }

Open in new window


However I got this error.

 dlaxL.png
0
Comment
Question by:Simbios
1 Comment
 
LVL 4

Accepted Solution

by:
guramrit earned 500 total points
ID: 36541297
There're some common mistakes in this code. You've to change it->

change
  var parent = parents.Single(parent => parent.Field<int>("UnitID") == group.Key);
to
  var parent = parents.Single(p => p.Field<int>("UnitID") == group.Key);

change
    var totalSurface = group.Sum(group => group.Field<int>("Surface"));
to
    var totalSurface = group.Sum(g => g.Field<int>("Surface"));

last thing, you cannot compare object of KeyValuePair<,> with null, because its a Structure (valuetype) returned by following code
   invalidGroup = childrenByParentID.FirstOrDefault ......

try using ToLookup instead of ToDictionary

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net uploading picture to a temporary folder 4 25
asp.net mvc 2 34
Can Angular be used with classic asp.net? 3 32
Validating textboxes in red square borders. 2 33
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question