如果存在记录,如何制作linq语句

时间:2013-09-18 06:12:39

标签: c# linq

我正在尝试这个,但它不起作用我怎么能纠正这个。

实际上我遇到两个条件,首先我需要检查记录是否存在,在第一种情况下,如果它存在我想要分配一些值不是来自edmx,在第二种情况如果记录存在我想从edmx分配值文件。

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

正如您所看到的,我正在使用任何一个来检查记录。

 _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());

1 个答案:

答案 0 :(得分:1)

你无法做你想做的事。它更干净,更容易理解:

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.