linq案例陈述

我需要有关linq(C#)中CASE语句的一些帮助:


osc_products.products_quantity =

      CASE 

         WHEN itempromoflag <> 'N' THEN 100000

         WHEN itemcat1 IN ('1','2','31') AND itemsalestatus = 'S' THEN 100000

         WHEN itemsalestatus = 'O' THEN 0

         ELSE cds_oeinvitem.itemqtyonhand - cds_oeinvitem.itemqtycommitted 

      END  

我开始转换为linq,(我仍在学习):


cdsDBDataContext db = new cdsDBDataContext();

  var query = from items in db.cdsItems

              where items.ItemHandHeldFlag.Equals("Y") && 

              items.ItemQtyOnHand -  items.ItemQtyCommitted > 0

  select items;

此查询将库存状态从生产更新为贸易站点。


慕尼黑的夜晚无繁华
浏览 351回答 3
3回答

UYOU

如果它只是LINQ中的CASE语句,请阅读(您的评论),然后再举一个例子。Int32[] numbers = new Int32[] { 1, 2, 1, 3, 1, 5, 3, 1 };var numberText =(&nbsp; &nbsp; from n in numbers&nbsp; &nbsp; where n > 0&nbsp; &nbsp; select new&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Number = n,&nbsp; &nbsp; &nbsp; &nbsp; Text =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n == 1 ? "One" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n == 2 ? "Two" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n == 3 ? "Three" : "Unknown"&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; });

慕慕森

到目前为止,这是我的进度,目前还没有任何工作,但这是一个开始:var query2 = from items in db.cdsItems&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where items.ItemTrackingCode.Equals("A") && (items.ItemQtyOnHand - items.ItemQtyCommitted) > 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;items,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;qty =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.ItemPromoFlag.Equals("1") ? "100000" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.ItemCat1.Equals("1") ? "100000" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.ItemSaleStatus.Equals("O") ? "0" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (items.ItemQtyOnHand - items.ItemQtyCommitted).ToString&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;};这种语法对我来说似乎很尴尬...我可能只是通过sql。

翻过高山走不出你

首先,选择要更新的项目。然后,使用常规C#更新它们。提交更改。&nbsp; &nbsp; var q = from osc in MyDataContext.osc_products&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join cds in cds_oeinvitem on osc.products_model equals cds.itemno into p&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where osc.Itemwebflag == 'Y'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select p;&nbsp; &nbsp; foreach (var item in q)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (item.itempromoflag != "N")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.products_quantity = 100000;&nbsp; &nbsp; &nbsp; &nbsp; else if ((new[] { 1, 2, 31 }.Contains(item.itemcat1)) && (item.itemsalestatus == 'S'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.products_quantity = 100000;&nbsp; &nbsp; &nbsp; &nbsp; else if (item.itemsalestatus == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.products_quantity = 0;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.products_quantity = item.itemqtyonhand - item.itemqtycommitted;&nbsp; &nbsp; }&nbsp; &nbsp; MyDataContext.SubmitChanges();
打开App,查看更多内容
随时随地看视频慕课网APP