我使用EF 4.0和我需要一個內部聯接,並用N外部連接 我就開始實施這一使用不同的方法,但陷入困境的一些點來實現查詢。內連接和實體框架外連接選項4.0
下面是兩個例子,我怎麼開始做這個使用ObjectQuery<'T'> and Linq to Entity
1)使用ObjectQuery<'T'>
我實現靈活的外部連接的,但我不知道如何執行在這種情況下,實體規則內連接(默認情況下包括(「辦法」)做外連接,但我需要通過內部編號加入)。
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)使用LINQ to實體,我需要有一種外(在GetRace(事端等))加入其中,我可以通過與實體清單包括),也是我需要執行正確的內部連接用實體規則
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();
}
可能有人對此有一些想法。