If Else与匿名对象相关联

本文关键字:对象 关联 Else If | 更新日期: 2023-09-27 18:02:06

如何在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[[teller。Denomination, DynamicFieldsDiagramLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'来键入'出纳员。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 ="收集"

If Else与匿名对象相关联

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

在本例中,您只想从数据库中获取数据,然后在。net客户端中执行转换。

要实现这一点,通过调用AsEnumerableIQueryable转换为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
        });

试试这个:

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