LINQ to SQL advice - why won't it work?
I'm trying to write a LINQ query that that simply gets the count of rows where a variable ('id') is equal to the JOB_GROUP stateme开发者_Python百科nt. Problem is, Visual Studio is returning an error on the ; at the end, saying 'Only assignment calls.....may be used as a statement'. Is there anything obvious wrong with my query?
var noofrows = from s in dc.QRTZ_JOB_DETAILs
where id == s.JOB_GROUP
select s.JOB_NAME.Count();
You need to wrap the linq query around parentheses before calling the Count() method.
var noofrows = (from s in dc.QRTZ_JOB_DETAILs
where id == s.JOB_GROUP
select s.JOB_NAME).Count();
More lightweight, and readable:
var count = dc.QRTZ_JOB_DETAILs.Count(x=>id == x.JOB_GROUP );
Alternatively, you could simply write:
var noofrows = dc.QRTZ_JOB_DETAILs.Count(s => id == s.JOB_GROUP);
精彩评论