gpt4 book ai didi

C# Entity Framework 4.1 Lambda Include - 仅选择特定的包含值

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:27 25 4
gpt4 key购买 nike

我正在 EF4.1 上执行 lambda 选择,包括我当前语句中的另一个相关 DBSet。

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

我有两个类:

//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; }
}

但我只想要每个 GameType 的下一次抽奖。本质上我想做类似的事情

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

但它给了我:

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.

这样的事情是可能的还是我需要在之后过滤结果?然后我还想按最新的 Draw.DrawDate 排序总结果。如果有人能告诉我正确的方法,我将不胜感激。

最佳答案

我觉得....

    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}

未经测试 - 但它可能会让您走上正确的道路...

关于C# Entity Framework 4.1 Lambda Include - 仅选择特定的包含值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6419523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com