【发布时间】: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 => q )似乎是这样。当您将其转换为Denomination而不是IEnumerable<Denomination>时,选择可能会返回一个集合。 -
你在做什么
.Select(q => q)? -
Denominations的选择是多余的。 -
旁注:您不必明确指定匿名对象的属性名称。如果不指定属性名称,则使用所选属性的属性名称。例如,您可以将
Id = a.Currency.Id,更改为a.Currency.Id。
标签: c# linq entity-framework if-statement casting