C# linq case 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/936028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:42:27  来源:igfitidea点击:

linq case statement

c#sqllinq

提问by Scott Kramer

I need some help with CASE statements in linq (c#):

我需要一些有关 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  

My start at converting to linq, (I'm still learning):

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

cdsDBDataContext db = new cdsDBDataContext();
  var query = from items in db.cdsItems
              where items.ItemHandHeldFlag.Equals("Y") && 
              items.ItemQtyOnHand -  items.ItemQtyCommitted > 0
  select items;
cdsDBDataContext db = new cdsDBDataContext();
  var query = from items in db.cdsItems
              where items.ItemHandHeldFlag.Equals("Y") && 
              items.ItemQtyOnHand -  items.ItemQtyCommitted > 0
  select items;

This query updates stock status from production to a commerce site.

此查询将库存状态从生产更新到商业站点。

采纳答案by KM.

If its just the CASEstatement in LINQyour after (read your comment) then an example of this is...

如果它只是LINQ 中CASE语句你之后(阅读你的评论)那么这样的一个例子是......

Int32[] numbers = new Int32[] { 1, 2, 1, 3, 1, 5, 3, 1 };

var numberText =
(
    from n in numbers
    where n > 0
    select new
    {
        Number = n,
        Text = 
        (
            n == 1 ? "One" :
            n == 2 ? "Two" :
            n == 3 ? "Three" : "Unknown"
        )
    }
);

回答by bruno conde

First, select the Items that you want to update. Then, update them in regular C#. Submit changes.

首先,选择要更新的项目。然后,在常规 C# 中更新它们。提交更改。

    var q = from osc in MyDataContext.osc_products
            join cds in cds_oeinvitem on osc.products_model equals cds.itemno into p
            where osc.Itemwebflag == 'Y'
            select p;

    foreach (var item in q)
    {
        if (item.itempromoflag != "N")
            item.products_quantity = 100000;
        else if ((new[] { 1, 2, 31 }.Contains(item.itemcat1)) && (item.itemsalestatus == 'S'))
            item.products_quantity = 100000;
        else if (item.itemsalestatus == 0)
            item.products_quantity = 0;
        else
            item.products_quantity = item.itemqtyonhand - item.itemqtycommitted;
    }

    MyDataContext.SubmitChanges();

回答by jrista

You are performing a bulk update, but link is purely a querying and object selection tool. Use the proper tool for the job...which in this case is definitely the database server.

您正在执行批量更新,但链接纯粹是一个查询和对象选择工具。使用合适的工具来完成这项工作……在这种情况下绝对是数据库服务器。

回答by KM.

use your single UPDATE statement in a stored procedure, will be better than doing a loop of updates on the application server.

在存储过程中使用您的单个 UPDATE 语句,比在应用程序服务器上执行更新循环要好。

回答by Denis Troller

There is no "Update" statement in Linq (whichever flavor you use, be it LinqToSQL or LinqToEntities).

Linq 中没有“更新”语句(无论您使用哪种风格,无论是 LinqToSQL 还是 LinqToEntities)。

Linq strictly provides a querying language.

Linq 严格提供了一种查询语言。

If you are using LinqToSQL and want to update data, you need to first query the context for the items you need to update, then loop over them to change their property and finally to call SubmitChanges to save the changes to the database.

如果您正在使用 LinqToSQL 并且想要更新数据,您需要首先查询需要更新的项目的上下文,然后遍历它们以更改它们的属性,最后调用 SubmitChanges 将更改保存到数据库。

回答by Scott Kramer

Here's my progress so far, not working at all yet, but is a start:

这是我到目前为止的进展,还没有工作,但这是一个开始:

var query2 = from items in db.cdsItems
             where items.ItemTrackingCode.Equals("A") && (items.ItemQtyOnHand - items.ItemQtyCommitted) > 0
             select new  {
                           items,
                           qty =
                                 (
                                    items.ItemPromoFlag.Equals("1") ? "100000" :
                                    items.ItemCat1.Equals("1") ? "100000" :
                                    items.ItemSaleStatus.Equals("O") ? "0" :
                                    (items.ItemQtyOnHand - items.ItemQtyCommitted).ToString
                                 )
                         };

This syntax seems so awkward to me... I might just pass-thru sql.

这种语法对我来说似乎很尴尬……我可能只是通过 sql。

回答by Vasu Balakrishnan