【问题标题】:Inner join and outer join options in Entity Framework 4.0Entity Framework 4.0 中的内连接和外连接选项
【发布时间】:2010-09-01 16:32:51
【问题描述】:

我使用的是 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();
    }

可能有人对此有一些想法。

【问题讨论】:

    标签: linq-to-sql entity-framework entity-framework-4


    【解决方案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();
        }
    

    【讨论】:

      【解决方案2】:

      这是一个post 处理 LINQ to Entities 中的内部联接。
      希望这将为您指明正确的方向。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 1970-01-01
        • 2016-06-07
        • 2021-12-26
        • 1970-01-01
        • 2014-08-10
        相关资源
        最近更新 更多