Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linq case statement

Tags:

c#

sql

linq

People also ask

What is a LINQ statement?

LINQ (Language Integrated Query) is a Microsoft programming model and methodology that essentially adds formal query capabilities into Microsoft . NET-based programming languages. LINQ offers a compact, expressive, and intelligible syntax for manipulating data.

Is LINQ where clause case sensitive?

LINQ has no concept of case sensitivity, it only cares about boolean evaluation. So if you want to ignore case, you should do something like: query = query.


If its just the CASE statement in LINQ your after (read your comment) then an example of this is...

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"
        )
    }
);

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.


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

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

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