Friday, June 17, 2011

Linq to Entity outer joins

Here is an example of a outer join using a linq to entity query:

public List GetList(string code, string name, int? groupID)
{
using (Entities entities = new Entities())
{
var query = from group in entities.Groups
join team in entities.Teams on group.ID equals team.fkGroupID into team_outer from team in team_outer.DefaultIfEmpty()
where (group.ID == groupID || groupID == null)
&& (team.name.ToLower().Contains(name.ToLower()) || string.IsNullOrEmpty(name))
&& (team.Code.ToLower().Contains(code.ToLower()) || string.IsNullOrEmpty(code))
select group;

return query.ToList();
}
}




Programming Microsoft® LINQ in Microsoft .NET Framework 4

Search Amazon.com for Linq to Entity

No comments: