linq case statement
Solution 1:
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"
)
}
);
Solution 2:
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.
Solution 3:
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();