Mik Skywalker
Mik Skywalker

Reputation: 29

Linq using Distinct() in C# Lambda Expression

SFC.OrderFormModifiedMonitoringRecords
   .SelectMany(q => q.TimeModify, w => w.DateModify)
   .Distinct()
   .OrderBy(t => t)
   .SelectMany(t => new { RowID = t.rowID, OFnum = t.OFNo });

It's Error did i missed something or is it Completely Coded The Wrong Way? After this i'm gonna use this on a Foreach method to gather up multiple data without the duplicates.

Upvotes: 1

Views: 296

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149078

The delegate you pass to SelectMany must return an IEnumerable and is for collapsing multiple collections into one. So yes, something's definitely wrong here. I think you've confused it with Select which simply maps one collection to another.

Without knowing what your goal is, it's hard to know exactly how to fix it, but I'm guessing you want something like this:

SFC.OrderFormModifiedMonitoringRecords
   .OrderBy(t => t.DateModify)
   .ThenBy(t => t.TimeModify)
   .Select(t => new { RowID = t.rowID, OFnum = t.OFNo })
   .Distinct();

Or in query syntax:

(from t in SFC.OrderFormModifiedMonitoringRecords
 orderby t.DateModify, t.TimeModify
 select new { RowID = t.rowID, OFnum = t.OFNo })
.Distinct();

This will order the records by DateModify then by TimeModify, select two properties, rowID and OFNo and return only distinct pairs of values.

Upvotes: 4

Related Questions