c# - Linq:How to make this easy with one line code? -


forget learnt linq,now need review it. , how works.now need this. please me .thank much.

var everymonthmoneysum = new everymonthmoneysum() {     m_01 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 1)             select o.signmoney).sum(),     m_02 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 2)             select o.signmoney).sum(),     m_03 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 3)             select o.signmoney).sum(),     m_04 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 4)             select o.signmoney).sum(),     m_05 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 5)             select o.signmoney).sum()+5,     ...........................     m_11 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 11)             select o.signmoney).sum(),     m_12 = (from o in temp.where(o => o.issign == (short) orderstateenum.success && o.signmonth == 12)             select o.signmoney).sum()  }; 

it sounds me dictionary:

var results = temp.where(o => o.issign == (short) orderstateenum.success)                   .groupby(o => o.signmonth)                   .todictionary(g => g.key, g => g.sum(o => o.signmoney)); 

note that won't populate dictionary entries "missing" months.

that give single collection, instead of 12 different variables. find that's better approach, don't know you're trying 12 values...

if want make sure dictionary populated, personally loop afterwards:

for (int = 1; <= 12; i++) {     if (!results.containskey(i))     {         results[i] = 0m;     } } 

it's not "clever", works. alternatively, populate array dictionary:

var array = new decimal[13]; // 0 12 *inclusive* foreach (var entry in array) {     array[entry.key] = entry.value; } 

there other ways of doing in linq statement, don't know how they'd play whatever linq provider you're using (we don't know) , they're more complex.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -