SQL: select case SellMode when 1 then SellNum when 2 then -SellNum when 3 then 0 end as SellNum from Sell 我知道如果只有2个case的话可以写成 ?: 的形式,但是有多个的话该怎么写呢?难道非要使用嵌套的 ?: 吗?
垃圾,你首先提问的是:
SQL: select case SellMode when 1 then SellNum when 2 then -SellNum when 3 then 0 end as SellNum from Sell
我知道如果只有2个case的话可以写成 ?: 的形式,但是有多个的话该怎么写呢?难道非要使用嵌套的 ?: 吗?
LINQ 语句是这样的:
var sell = from s in db.Sell
group s by new { s.SellStore, s.GoodID } into sInto
select new
{
StoreID = sInto.Key.SellStore,
GoodID = sInto.Key.GoodID,
SellNum = sInto.Sum(item => NumberConverter(item.SellMode ?? new char(), Convert.ToInt32(item.TotalSellNum ?? 0)))
};
select case SellMode when 1 then SellNum when 2 then -SellNum when 3 then 0 end as SellNum from Sell
可以这样:
[Code=C#]
var query = from c in Sell
select new {
SellMode = NumberConverter(c.SellMode);
}
public int NumberConverter(int sellMode)
{
switch (sellMode)
{
case 1
return sellMode;
case 2
return 0 - sellMode;
default:
return 0;
}
}
[/Code]