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

Triggers in SQL

Just a note for all of you that are banging their heads against the laptop in front of you, there is no there is no Updated dummy table in SQL Server triggers. For an  AFTER UPDATE trigger, you need to use either the Deleted dummy table that contains the old values or the Inserted table that contains the new values.

It does make perfect sense once you know that this is the case, but considering SQL is trying to be a 'friendly' language, once again it has not gone far enough and created (short-lived) confusion on my part.