c# - Access foreign key data linq -
i have simple requirement if done via sql query. have table category id,name table b categoryitems has foreign key of categoryid,name. using linqdatasource simple select statements show data table a.
select * (simple)   i have requirement want "not show" categories in first place not have items associated them i. e (select count(*) b categoryid="")>0
very easy modifying sql statement, wondering if can done possible accessing foreign key relationship data using out of box linq functionality , applying validation.
just keen on it!..
thanks ton!
you want use any() method on category.categoryitems
any() returns true if count > 0 , false if count == 0
// select categories have @ least 1 categoryitem. ienumerable<category> categorieswithitems = context.categories.select(x => x.categoryitems.any());   for linqdatasource want use selecting event handler. msdn.
the aspx:-
<asp:linqdatasource id="linqdatasource1"                 runat="server"                   contexttypename="mydatacontext"                 onselecting="linqdatasource1_selecting"> </asp:linqdatasource>   the method:-
public void linqdatasource1_selecting(object sender, linqdatasourceselecteventargs e) {      e.result = categorieswithitems = context.categories.select(x => x.categoryitems.any());     }      
Comments
Post a Comment