在LINQ中有所区别

时间:2012-02-13 10:06:11

标签: asp.net sql linq

我在LINQ格式中翻译此查询时遇到问题:

select Version, count(distinct(idUser)) from HistoUsers
group by Version

到目前为止我所拥有的是:

public static List<VersionsUsed> GetNumberOfCompaniesUsingEachVersions2()
{
    var foundUsers = (from hu in sdt.DataContext.HistoUsers

                      group hu by new { hu.Version, hu.IdUser } into g
                      select new VersionsUsed { nameVersion = g.Key.Version, NumberOfCompaniesUsingThisVersion = g.Count((g.Key.IdUser).distinct()) });

    return foundUsers.ToList();
}

似乎我在distinct()上有语法问题 谁可以帮我这个事 ? 提前谢谢

5 个答案:

答案 0 :(得分:0)

from hu in sdt.DataContext.HistoUsers
group hu by new { hu.Version, hu.IdUser } into g
select new VersionsUsed 
 {  
   nameVersion = g.Key.Version, 
   NumberOfCompaniesUsingThisVersion = g.Select(c=>c.IdUser).Distinct().Count() 
 }

答案 1 :(得分:0)

您可以使用LINQER:它从等效的SQL语句生成LINQ语句。您可以从http://www.sqltolinq.com/downloads下载10天试用版。

答案 2 :(得分:0)

也许是这样的:

select new VersionsUsed 
        { 
            nameVersion = g.Key.Version, 
            NumberOfCompaniesUsingThisVersion = g.Select(a=>a.IdUser).Distinct().Count()) 
        };

答案 3 :(得分:0)

你的独特陈述必须是这样的: Distinct(...)

上面的第一个字母

答案 4 :(得分:0)

而不是distinct使用关键字first。它会起作用:

var foundUsers = (from hu in sdt.DataContext.HistoUsers
   group hu by new { hu.Version, hu.IdUser } into g
   select new VersionsUsed { nameVersion = g.Key.Version, NumberOfCompaniesUsingThisVersion = g.Count((g.Key.IdUser).First()) });
return foundUsers.ToList();