C#实体框架4.1的lambda包括 - 只选择特定的值,包括 [英] C# Entity Framework 4.1 Lambda Include - only select specific included values

查看:104
本文介绍了C#实体框架4.1的lambda包括 - 只选择特定的值,包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的lambda选择EF4.1,包括另一个相关DBSet在我当前语句。

 返回dbEntity .GameTypes.Include(A => a.Draws)
。凡(D => d.IsActive == true)而
.ToList();



我有两个类:



<$布尔指p $ p> //简化了类
公共类游戏类型
{
公众可空<版本; IsActive {搞定;组; }
公共虚拟的ICollection<绘图>绘制{搞定;组; }
}

公共类抽奖
{
公众诠释DrawID {搞定;组; }
公众诠释GameTypeID {搞定;组; }
公众的System.DateTime DrawDate {搞定;组; }
}



不过,我只希望每个游戏类型的下一个即将到来的平局。基本上我想要做这样的事情。



 返回dbEntity.GameTypes.Include(A => a.Draws.Where(AW = > aw.DrawDate> System.DateTime.Now)
.OrderBy(AO => ao.DrawDate)
。首先())
。凡(D = D 1和D。 IsActive ==真)
.ToList();



但它给了我:




包含路径表达式必须引用的类型定义的导航属性。借鉴导航属性和集合导航属性选择运营商dottedpaths。




是这样的可能还是我需要筛选事后结果?那么我也很喜欢用最新的Draw.DrawDate订购的总成绩。如果有人能告诉我正确的方法,我会真实地gratefull。


解决方案

我想......

 <在dbEntity.GameTypes.Include code>从克(绘制)
,其中g.​​IsActive
令d = g.Draws.Where(O => o.DrawDate>系统。 DateTime.Now)
.OrderBy(O => o.DrawDate)
。取(1)//需要保持一个集合
选择新的游戏类型{IsActive = g.IsActive,绘制= D}

未经检验的 - 但它可能让你在正确的道路上......


I'm doing a lambda select on EF4.1, including another related DBSet in my current statement.

 return dbEntity.GameTypes.Include(a => a.Draws)
                           .Where(d => d.IsActive == true )
                           .ToList();

I've got two classes:

//simplified versions of the classes
public class GameType
{
 public Nullable<bool> IsActive { get; set; }
 public virtual ICollection<Draw> Draws { get; set; }
}

public class Draw
{
 public int DrawID { get; set; }
 public int GameTypeID { get; set; }
 public System.DateTime DrawDate { get; set; }
} 

But I only want the next upcoming draw for each GameType. Essentially I want to do something like

 return dbEntity.GameTypes.Include(a => a.Draws.Where(aw => aw.DrawDate > System.DateTime.Now)
                                               .OrderBy(ao => ao.DrawDate)
                                               .First())
                           .Where(d => d.IsActive == true )
                           .ToList();

But it gives me:

The Include path expression must refer to a navigation property defined on the type. Use dottedpaths for reference navigation properties and the Select operator for collection navigation properties.

Is something like this possible or would I need to filter the result afterwards? I'd then also like to order the total result by the latest Draw.DrawDate. If anyone could show me the proper way I'd be trully gratefull.

解决方案

I think....

    from g in dbEntity.GameTypes.Include("Draws")
   where g.IsActive
     let d = g.Draws.Where(o => o.DrawDate > System.DateTime.Now)
                    .OrderBy(o => o.DrawDate)
                    .Take(1)       // Needs to stay a collection
  select new GameType {IsActive = g.IsActive, Draws = d}

untested - but it might get you on the right path...

这篇关于C#实体框架4.1的lambda包括 - 只选择特定的值,包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆