1
如何計算Linq中一列的計數。例如:linq中一列的計數
select count(productid) from products
我將如何寫在linq。
這是這樣嗎?
var query = (from p in context.products select p).count()
請建議。
如何計算Linq中一列的計數。例如:linq中一列的計數
select count(productid) from products
我將如何寫在linq。
這是這樣嗎?
var query = (from p in context.products select p).count()
請建議。
如何:
var count = context.Products.Count(p => p.ProductId != null);
這是假設我理解你想在指定的計數部分ProductId
點。 不是在產品行中是ProductId,這是非常不尋常的。如果你只是想行數可言,只需使用:
var count = context.Products.Count();
如果你以後不同值,你會做這樣的事情:
var count = context.Products.Select(p => p.ProductId)
.Distinct()
.Count();
如果我想使用從符號而不是擴展方法符號? – DotnetSparrow 2011-04-05 20:17:15
@DotnetSparrow:那麼你不能使用Count調用本身的查詢表達式。你可以使用'(來自上下文中的p.Products,其中p.ProductId!= null選擇p).Count()'爲第一個,或者(從context.Products中選擇p.ProductId).Distinct()。Count ()'第二 - 但對於這樣的查詢,我更喜歡擴展方法。 – 2011-04-05 20:23:29