Using Linq to SQL, how do I find min and max of a column in a table?

前端 未结 5 1259
情书的邮戳
情书的邮戳 2020-12-29 03:40

I want to find the fastest way to get the min and max of a column in a table with a single Linq to SQL roundtrip. So I know this would work in two roundtrips:



        
5条回答
  •  孤独总比滥情好
    2020-12-29 04:15

    As stated in the question, this method seems to actually generate optimal SQL code, so while it looks a bit squirrely in LINQ, it should be optimal performance-wise.

    from row in MyTable  
    group row by true into r  
    select new {  
        min = r.Min(z => z.FavoriteNumber),  
        max = r.Max(z => z.FavoriteNumber)  
    } 
    

提交回复
热议问题