Wednesday 15 July 2015

Tricky LINQ and nested collection -


I am trying to find an easy way to get the following code and in as few lines as possible I can include it. Ideally, I have an IDictionary & gt; Out of this

  var report = db.report In the form of selection (get rid = & gt; rid.ind == report id); Dictionary & lt; String, list & lt; String & gt; & Gt; Notes = New Dictionary & lt; String, list & lt; String & gt; & Gt; (); Foreach (subreport on report.SubReports) {foreach (Subreportitem in subreport.SubReportItems) {Notes [subreportitem.Title] = New list of & lt; String & gt; (); Foreign currency (Subrepresentation. SRR report diversified note in ITM notes) {Notes [subrepresentation Title]. Add notes (notes notes); }}}   

Ideally, I wanted to do something like this: DB from the report. Reports where report.ID == report released in subreports report. SubReports from subreportitems from subreportitems. SubReportItems subreportitems.SubReportItemNotes // Notes Make sure to select the notes in the essential dictionary ...

This should be the equivalent:

  db.Reports .Where (rpt = & gt; rpt.id == reportId) .Cast & LT; Report & gt; () .SelectMany (rpt = & gt; rpt.SubReports) .SelectMany (subRpt = & gt; subRpt.SubReportItems) .toDictionary (Mr. => gt; sri.Title, Mr. = & gt; s.SubReportItemNotes.SelectMany (note = & Gt; note. Note: Details);   

Notes:
  1. Selects the first line of your code, using . Rid = = gt; Raida.id == reportId) , but I think it should have been where should be selected instead of , else you would Null will end with the collection because s choice will be the result type bool And for the report , output will be null for each.
  2. This only works if your subreport item titles s are unique, it is conceivable that a report may be 10 subreports and those subreports see two or In the midst of more SubReportItems , with the same title . If so, then you have to think about it a bit, otherwise you will get a duplicateKeyException when you try to add a title that is already in the dictionary. Explanation:

    Explanation:

    Actually, we are taking a set of reports and implementing this condition that we only want reports where the ID is desired personally From, I will put it in a separate line and instead of SingleOrDefault I will use where because I expect only one result.

    Next, we .cust and lieutenant; Report & gt; Just because you're using as a report , so I think you need it. It can be in unnecessary and unnecessary behavior.

    First . Select multiple calls to get all subrps all reports . Again, we can rarely have an report objects at this point.

    Now we have a bunch of sub-reports , but we really want to get all the SubReportItems , then we will get another They use SelectMany .

    Now that we have all the SubReportItem s, all (1) report s, from all SubRoad s, we For each SubReportItem we create a key from the Title property, and then for the value we use a final SelectMany Note: Current all existing SubReportItemNote s. Objects related to

No comments:

Post a Comment