LINQ COUNT on multiple columns

后端 未结 3 1408
遇见更好的自我
遇见更好的自我 2021-01-12 15:14

If I have a table with a title column and 3 bit columns (f1, f2, f3) that contain either 1 or NULL, how would I write the LINQ to return the title with the count of each bit

3条回答
  •  清酒与你
    2021-01-12 15:54

    I think this is where LINQ falls down. If you want efficient use the SQL, if you want nice code, use LINQ.

    You could always execute the query directly, since you know the SQL already.

    class TitleCount {
        public string Title;
        public int Count1;
        public int Count2;
        public int Count3;
    }
    
    DataContext dc = new DataContext("Connection string to db");
    
    IEnumerable query = dc.ExecuteQuery(
        @"SELECT title, 
                 COUNT(f1) as Count1, 
                 COUNT(f2) as Count2, 
                 COUNT(f3) as Count3 
           FROM myTable GROUP BY title");
    

提交回复
热议问题