views:

61

answers:

2

Can anyone help out with what the C# code would be to implement this SQL as Entity Framework Linq in the abbreviated form? (e.g. where you have the "." notation such as xxx.where(... etc)

SELECT PN.Name, Sum(U.Amount)
FROM Usages as U, ProcessNames as PN
WHERE PN.Id == U.ProcessNameId 
   AND U.Datetime BETWEEN '2010-01-08' AND '2010-10-11'
Group By PN.Name
+1  A: 

Try this (I don't have your code so I'm getting no compiler help):

from u in context.Usages
join pn in context.ProcessNames on pn.Id equals u.ProcessNameId
where u.Datetime >= new DateTime(2010, 1, 8) && u.Datetime <= new DateTime(2010, 10, 11)
group pn by pn.Name into g
select new { Name = pn.Name , sum = g.Sum(u => u.Amount) };

That is the query expression version of it. To get the lambda based syntax (as I believe you are asking for this question), put the query into LinqPad and run it. Then click on the lambda tab in LinqPad and it will show you that syntax of the above query as if you had written it with lambda expressions (i.e., where you have the "." notation) and not a query expression.

Steve Michelotti
SQL `betweens` are inclusive, so your datetime range checks need correcting to match.
Will
@Will - Fixed the inclusions.
Steve Michelotti
+3  A: 
Morteza Manavi
SQL `betweens` are inclusive, so your datetime range checks need correcting to match.
Will
I updated the queries. Thank you!
Morteza Manavi
excellent thanks - which approach do you think is easier to understand program in our of curiosity? method based or query based?
Greg
You are very welcome. I Personally like the Method-Based syntax better, although Query Expression syntax is more readable and of course easier to write, but it's yet another level of abstraction and I think if we make ourselves familiar with Method Based syntax, we can always turn it into Query Expression but that's just my opinion.
Morteza Manavi