【问题标题】:If Else in linq with anonymous objectIf Else in linq with anonymous object
【发布时间】:2015-07-11 05:15:11
【问题描述】:

如何在 LINQ 查询中执行 If Else 条件?

cashierdata.useDenominations 是布尔类型,我正在对同一个对象进行强制转换。 类似的东西

IQueryable<CashierBalance> icashierBalance = _cashierDataManagement.GetIQueryableCashierBalance();
        var currencies = icashierBalance.Select(a => new
        {
            Id = a.Currency.Id,
            Name = a.Currency.Name,
            Simbol = a.Currency.Symbol,
            ShorName = a.Currency.ShortName,
            RoundingUp = a.Currency.RoundingUp,
            RoundingDown = a.Currency.RoundingDown,
            DenominationMin = a.Currency.DenominationMin,
            Denominations = cashierdata.useDenominations ? (Denomination) a.Currency.Denominations.Select(q => q )

             : (Denomination) null


        });

来自 api 的响应

无法将类型“System.Collections.Generic.IEnumerable`1[[Tellers.Denomination, DynamicFieldsDiagramLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]”转换为类型“Tellers.Denomination”。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型。

没有投射

IQueryable<CashierBalance> icashierBalance = _cashierDataManagement.GetIQueryableCashierBalance();
        var currencies = icashierBalance.Select(a => new
        {
            Id = a.Currency.Id,
            Name = a.Currency.Name,
            Simbol = a.Currency.Symbol,
            ShorName = a.Currency.ShortName,
            RoundingUp = a.Currency.RoundingUp,
            RoundingDown = a.Currency.RoundingDown,
            DenominationMin = a.Currency.DenominationMin,
            Denominations = cashierdata.useDenominations ?  a.Currency.Denominations.Select(q => q )

             :  null


        });

例外

不支持嵌套查询。 Operation1='案例' Operation2='收集'

【问题讨论】:

  • (Denomination) a.Currency.Denominations.Select(q =&gt; q ) 似乎是这样。当您将其转换为 Denomination 而不是 IEnumerable&lt;Denomination&gt; 时,选择可能会返回一个集合。
  • 你在做什么.Select(q =&gt; q)
  • Denominations 的选择是多余的。
  • 旁注:您不必明确指定匿名对象的属性名称。如果不指定属性名称,则使用所选属性的属性名称。例如,您可以将 Id = a.Currency.Id, 更改为 a.Currency.Id

标签: c# linq entity-framework if-statement casting


【解决方案1】:

问题是您的IQueryable 正试图解析您的表达式并将其转换为 SQL 表达式。这超出了 Entity Framework 的能力(具体来说,它无法将可枚举的对象分配给 Denominations)。

在这种情况下,您只想从数据库中获取数据,然后在 .NET 客户端中执行转换。

要实现这一点,请通过调用 AsEnumerable 将您的 IQueryable 转换为 LINQ2Objects

IQueryable<CashierBalance> icashierBalance = _cashierDataManagement.GetIQueryableCashierBalance();
        var currencies = icashierBalance
                         .AsEnumerable()
                         .Select(a => new
        {
            Id = a.Currency.Id,
            Name = a.Currency.Name,
            Simbol = a.Currency.Symbol,
            ShorName = a.Currency.ShortName,
            RoundingUp = a.Currency.RoundingUp,
            RoundingDown = a.Currency.RoundingDown,
            DenominationMin = a.Currency.DenominationMin,
            Denominations = cashierdata.useDenominations ? a.Currency.Denominations.Select(q => q ) : null
        });

【讨论】:

    【解决方案2】:

    试试这个:

    IQueryable<CashierBalance> icashierBalance = _cashierDataManagement.GetIQueryableCashierBalance();
    var currencies = icashierBalance.Select(a => new
    {
        Id = a.Currency.Id,
        Name = a.Currency.Name,
        Simbol = a.Currency.Symbol,
        ShorName = a.Currency.ShortName,
        RoundingUp = a.Currency.RoundingUp,
        RoundingDown = a.Currency.RoundingDown,
        DenominationMin = a.Currency.DenominationMin,
        Denominations = cashierdata.useDenominations ? a.Currency.Denominations : null
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-07
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多