c# - LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent) -
After using many Google search and code, I have been stumped on a complex C # LINQ-to-object problem that is in SQL I
ROW_NUMBER () it's easy to solve ... divide by function and a subquery or two
Here in the words, what I'm trying to do in the code I am removing duplicate documents from the underlying requirement list:
- First, a list group (Document.Title, Document.Sou RceId), thus assuming a (simplified) class definition:
class document {string title; Int sorid; // sources are preferred (ID = 1 is better than ID = 2)}
- Assign an index to each document within that group (like index 0 == 1 From this document, the first document from this title, Index 1 = from this source, from this title to other documents, etc.) I would like the equivalent of ROW_NUMBER () in SQL!
- Now for the group (document title, index), where the index was counted in step # 2, return only one document: one with the lowest document. SourceId
Step # 1 is easy (e.g. codepronet.blogspot.com/2009/01/group-by-in-linq.html), but I'm stumped at step # 2 # 3. I can not construct the red-round-free C # LINQ query to solve all the three steps.
On the post of Anders Heelsberg, I think the answer to step # 2 and # 3 can be correct if I get the syntax right.
I pose an external local variable Prefer to avoid using it, as suggested on slodge.blogspot.com. 2009/01 / adding-row-number-using-linq-to-objects.html, because this solution breaks if the external variable is modified.
The customization, group-by-title phase can be done first, so the small number of "Inner" grouping (prior to the source to calculate the index, then to filter duplicates by index) Objects in each "Title" group can work, since the documents are generally less than 100 in each by-title group. I really do not want an EN 2 solution!
I can certainly solve this problem with nested forecheck loops, but it seems like the problem is what should be simple with LINQ.
Any thoughts?
I think that Jepbochchi remembers that you want to group your pairs of values (title + Source ID is the title + index) Here is a LINQ query (most of the) solution:
var selectedFew = new in docs group dock {G.K. in GK by new {doc.Title, doc.SourceId} .C. Select in. , I) = & gt; New {doc = D, index = i}) Select group docIndex by new {docIndex.Doc.Title, docIndex.index} g.Agregate ((a, b) => (a.ococ.sourceId & lt) ; = B.Doc.SourceId)? A: b);
Firstly we use the title + group by SourceId (I use an anonymous type because the compiler creates a good hash code for the grouping lookup). Then we choose to attach the grouped index into the Use Document, which we use in our second group. Lastly, we choose the following source ID for each group.
Looking at this input:
var docs = new [] {new {title = "ABC", SourceId = 0}, new {title = "ABC" New {title = "123", SourceId = 7}, New {title = "SourceId ="}, New {title = "ABC", SourceId = 2}, New {title = "123", SourceId = 7} 123 ", SourceId = 7}, new {title =" 123 ", SourceId = 5}, new {title =" 123 ", SourceId = 5},};
I get this output:
{doc = {title = ABC, sourced = 0}, index = 0} {doctor = {title = 123 index = 5}, index = 0} {doctor = {title = 123, source id = 5}, index = 1} {doctor = {title = 123, sourced = 7}, index = 2}
< / Ex>Update: I had previously seen how to group your question with the title. You can do this by using a subquery on your heading groups:
var selectedFew = Document group in doc by doc.Title in the title group by docWithIndex (in the title by the Doctor by Doc Group Group Doctor. IdGroup in idGroup.Select (d, i) => New {doc = D, index = i}) Select the group index group by docIndex.Inx Index group index by docIndex. Aggregate (A, B) => (A.Doc.SourceId & lt; = b.Doc.SourceId)? A: b) Doctor selectindx;
Comments
Post a Comment