0

How to convert the following SQL query to a lambda expression?

select MAX(ID),ProcessInstanceID from tblKGWorks 
where FormID = 2598 and FormTypeID =2306
group by ProcessInstanceID
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
morteza
  • 1
  • 1
  • 1
    As always, it's useful to shows what you've tried so far. Are you using EF? LINQ to SQL? Do you have that part of it sorted already? Do you know how to do the filtering? The grouping, but not the projection perhaps? – Jon Skeet Apr 11 '15 at 14:37
  • Have you tried anything? – Ehsan Sajjad Apr 11 '15 at 14:38
  • used ado.net entity data model – morteza Apr 11 '15 at 14:54
  • var bla = from s in kd.tblKGWorks group s by new { s.ProcessInstanceID} into g select new { g.Key.ProcessInstanceID, g.Max(s=>s.ID)}; – morteza Apr 11 '15 at 16:02
  • Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. – morteza Apr 11 '15 at 16:03
  • Please edit your question in order to include what you've tried so far along with any errors you get, instead of placing this sort of information in comments. – Giorgos Betsos Apr 11 '15 at 16:07

1 Answers1

0

Try it:

var qry = tblKGWorks
          .Where(gk=>gk.FormID == 2598 && gk.FormTypeID == 2306)
          .GroupBy(x=>x.ProcessInstanceID)
          .Select(grp=>new{ID = grp.Max(a=>a.ID), ProcessInstanceID = grp.Key});

More at: 101 Linq Samples

Maciej Los
  • 8,468
  • 1
  • 20
  • 35