【问题标题】:Where for list property Entity Framework列表属性实体框架的位置
【发布时间】:2018-09-19 14:12:16
【问题描述】:

我有两个实体 GasStationSummary 包含FuelSummary 的列表

public class GasStationSummary
{ ...

    public virtual List<FuelSummary> FuelSummary { get; set; } = new List<FuelSummary>();
}
 public class FuelSummary
{
    public int GsSummaryId { get; set; }

    public int FuelId { get; set; }

   ...
}

我有加油站:

this.repository.ListAsync().Include(x => x.FuelSummary)

我想获取带有 FuelId 等于 1 的燃料类型列表的加油站

有没有办法使用实体框架来做到这一点?

【问题讨论】:

  • Where(x =&gt; x.FuelId == 1)?
  • @HimBromBeere 不,x 没有属性 FuelId - x.FuelSummary[i] 有它
  • 仅供参考,拥有一个名称与其包含的类型相同的列表属性非常令人困惑。由于它是一个集合类型,您可以考虑将其设为复数:GasStationSummary.FuelSummaries

标签: c# .net entity-framework orm


【解决方案1】:

你可以使用投影

   this.repository.ListAsync().Where(u => u.FuelSummary.Any(e => e.FuelId == 1)
            .Select(x => new
            {
                x,
                Fuels = x.FuelSummary.Where(e => e.FuelId == 1)
            });

或库Entity Framework PlusIncludeFilter

【讨论】:

    猜你喜欢
    • 2010-10-03
    • 1970-01-01
    • 2014-12-05
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2013-01-24
    • 2013-09-06
    相关资源
    最近更新 更多