C# 如果存在记录,如何生成linq语句
我正在尝试这一点,但它不工作,我如何才能纠正这一点 实际上,在这两种情况下,我都面临两个条件:首先,我需要检查记录是否存在,第一种情况下,如果它存在,我希望分配一些值,而不是来自edmx,第二种情况下,如果记录存在,我希望分配来自edmx文件的值C# 如果存在记录,如何生成linq语句,c#,linq,C#,Linq,我正在尝试这一点,但它不工作,我如何才能纠正这一点 实际上,在这两种情况下,我都面临两个条件:首先,我需要检查记录是否存在,第一种情况下,如果它存在,我希望分配一些值,而不是来自edmx,第二种情况下,如果记录存在,我希望分配来自edmx文件的值 IsPriceApplied = cn.OrderDressings.Any(x => x.OrderID == OrderID && x.OrderItemID == ProductID) ?(from yy in cn.Ord
IsPriceApplied = cn.OrderDressings.Any(x => x.OrderID == OrderID && x.OrderItemID == ProductID) ?(from yy in cn.OrderDressings where yy.OrderID==OrderID && yy.OrderItemID==ProductID select yy.IsPriceApplied):Do Nothing
正如你所看到的,我正在使用any来首先检查记录
_allAppliedItemList.AddRange((from xx in DressingItems
where xx.DressingInfo.CatID == item.CategoryID
select new btnObject()
{
CustomizationType = CategoryType.Dressing,
BtnName = xx.DressingInfo.Description,
Price = (double)xx.DressingInfo.MainPrice,
IsSelected = xx.IsDefault,
MaxLimit = item.DefaultFreeCount,
CategoryID = xx.DressingInfo.CatID,
IsDefaultLimit = item.IsDefaultLimit,
ID = xx.DressingInfo.DressingID,
ButtonColor = cn.OrderDressings.Any(x => x.OrderID == OrderID && x.OrderItemID == ProductID)
? ButtonColor.Green
: ButtonColor.Red,
catName = item.CatName,
IsPriceApplied = cn.OrderDressings.Any(x => x.OrderID == OrderID && x.OrderItemID == ProductID)
? (from yy in cn.OrderDressings where yy.OrderID==OrderID && yy.OrderItemID==ProductID select yy.IsPriceApplied)
: noting
}
).ToList());
你不能做你想做的事。它更干净,更容易理解,如下所示:
bool priceApplied = cn.OrderDressings.Any(x => x.OrderID == OrderID && x.OrderItemID == ProductID);
if (priceApplied) {
var query = (from yy in cn.OrderDressings
where yy.OrderID == OrderID &&
yy.OrderItemID == ProductID
select yy.IsPriceApplied);
// do stuff.
}
// else.. do nothing.
哇,你又来了,为什么不先描述一下你的问题,更多关于你想要什么的细节。当您的代码不可理解时,它对描述您的问题没有任何帮助。好的,我首先描述它。但我正在创建一个它不支持的对象。再次查看我的编辑帖子。或者我应该采用alternet方法。