Inner join and outer join options in Entity Framework 4.0
- by bigb
I am using EF 4.0 and I need to implement query with one inner join and with N outer joins
I started to implement this using different approaches but get into trouble at some point.
Here is two examples how I started of doing this using ObjectQuery<'T' and Linq to Entity
1)Using ObjectQuery<'T' I implement flexible outer join but I don't know how to perform inner join with entity Rules in that case (by default Include("Rules") doing outer join, but i need to inner join by Id).
public static IEnumerable<Race> GetRace(List<string> includes, DateTime date)
{
IRepository repository = new Repository(new BEntities());
ObjectQuery<Race> result = (ObjectQuery<Race>)repository.AsQueryable<Race>();
//perform outer joins with related entities
if (includes != null)
foreach (string include in includes)
result = result.Include(include);
//here i need inner join insteard of default outer join
result = result.Include("Rules");
return result.ToList();
}
2)Using Linq To Entity I need to have kind of outer join(somethin like in GetRace()) where i may pass a List with entities to include) and also i need to perform correct inner join with entity Rules
public static IEnumerable<Race> GetRace2(List<string> includes, DateTime date)
{
IRepository repository = new Repository(new BEntities());
IEnumerable<Race> result = from o in repository.AsQueryable<Race>()
from b in o.RaceBetRules
select new
{
o
});
//I need here:
// 1. to perform the same way inner joins with related entities like with ObjectQuery above
//here i getting List<AnonymousType> which i cant cast to
//IEnumerable<Race> when i did try to cast like
//(IEnumerable<Race>)result.ToList(); i did get error:
//Unable to cast object of type
//'System.Collections.Generic.List`1[<>f__AnonymousType0`1[BetsTipster.Entity.Tip.Types.Race]]'
//to type
//'System.Collections.Generic.IEnumerable`1[BetsTipster.Entity.Tip.Types.Race]'.
return result.ToList();
}
May be someone have some ideas about that.