Параметры внутреннего и внешнего объединения в Entity Framework 4.0 - PullRequest
0 голосов
/ 01 сентября 2010

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

Вот два примера того, какЯ начал делать это, используя 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();
    }

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

Ответы [ 2 ]

1 голос
/ 09 января 2011
    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 голосов
/ 02 сентября 2010

Вот сообщение , касающееся внутреннего присоединения в LINQ to Entities.Надеюсь, это укажет вам правильное направление.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...