I have a table, let's call it Record. Contains: ID (int) | CustID (int) | Time (date and time) | Data (varchar)
I need the last (last) entry for each client:
SQL
select * from record as i group by i.custid having max(id);
LINQ version 1
dgvLatestDistinctRec.DataSource = from g in ee.Records
group g by g.CustID into grp
select grp.LastOrDefault();
This causes an error:
System.NotSupportedException was not handled by Message = LINQ user code for entities does not recognize the method "Faizan_Kazi_Utils.Record LastOrDefault [Record] (System.Collections.Generic.IEnumerable`1 [Faizan_Kazi_Utils.Record]) ', and this method cannot be translated into repository expression. Source = System.Data.Entity
LINQ version 2
var list = (from g in ee.Records
group g by g.CustID into grp
select grp).ToList();
Record[] list2 = (from grp in list
select grp.LastOrDefault()).ToArray();
dgvLatestDistinctRec.DataSource = list2;
This works, but is inefficient, because it loads ALL records from the database into memory and then retrieves only the last (last member) of each group.
- LINQ, SQL?