您想使用该Any()
方法Category.CategoryItems
Any()
返回true
ifCount > 0
和false
if theCount == 0
// Select only the Categories which have at least one CategoryItem.
IEnumerable<Category> categoriesWithItems = Context.Categories.Select(x => x.CategoryItems.Any());
对于linqdatasource
您想要使用的Selecting event handler
. 微软_
aspx:-
<asp:LinqDataSource ID="LinqDataSource1"
runat="server"
ContextTypeName="MyDataContext"
OnSelecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
方法:-
public void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = categoriesWithItems = Context.Categories.Select(x => x.CategoryItems.Any());
}