2010-09-01 4 views
0

Я использую EF 4.0, и мне нужно реализовать запрос с одним внутренним соединением и с N внешними соединениями Я начал реализовывать это с использованием разных подходов, но в какой-то момент столкнулся с проблемами.Параметры внутреннего соединения и внешнего соединения в Entity Framework 4.0

Вот два примера, как я начал делать это с помощью ObjectQuery<'T'> and Linq to Entity

1) Использования ObjectQuery<'T'> я реализую гибкие внешнее соединение, но я не знаю, как выполнить внутреннее соединение с правилами сущностей в этом случае (по умолчанию Включить («Правила»), выполняющие внешнее соединение, но мне нужно внутреннее соединение с помощью 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) Использование Linq To Entity Мне нужно иметь вид внешнего соединения (что-нибудь, как в 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(); 
    } 

Может быть, у кого-то есть идеи об этом.

ответ

1
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); 


     //Perform inner join with "Rules" table 
     result = (ObjectQuery<Race>)result.Include("Rules").Where(x => x.RaceBetRules.Any()); 

     result = (ObjectQuery<Race>)result.OrderBy(x => x.RaceDate); 


     return result.ToList(); 
    } 
0

Описание: post, связанный с Inner Join в LINQ to Entities.
Надеюсь, это укажет вам правильное направление.

Смежные вопросы