Pages - Menu

2019年2月19日 星期二

[LinQ]LINQPad快速測試,建立資料篇

測試LinQ的最快方法,
如果不是連資料庫撈資料測試,
只是要自己玩玩的話。
Language 選擇 C# Program

void Main()
{
     test();
}
class aa
{
     public string id{get;set;}
     public string Name{get;set;}
     public int Value{get;set;}
}
private void test()
{
     var li = new List<aa>();
     aa one = new aa();
    
     one.id="A";
     one.Name="甲";
     one.Value=1080101;   
    
     li.Add(one);
    
     li.Add(new aa{ id="A",Name="乙",Value=1080102 });
     li.Add(new aa{ id="B",Name="丙",Value=7 });
     li.Add(new aa{ id="B",Name="丁",Value=1 });
     li.Add(new aa{ id="B",Name="戊",Value=10 });
        
     var query = from p in li
         group p by new{p.id} into g
         select new{
             g.Key.id,
             Name = (from g2 in g where g2.Value == g.Max(s => s.Value) select g2.Name).Max(),
             Value= (from g2 in g where g2.Value == g.Max(s => s.Value) select g2.Value).Max()
         };
     query.Dump();
    
     var query3 = li.GroupBy(p=>p.id).Max(p=>p.Value);
    
     var query2 = from p in li
         group p by new{p.id} into g
         select g;
     query2.Dump();
}

參考資料:Writing Methods and Classes in LINQPad

沒有留言:

張貼留言