C# LINQ Lambda 查询,具有 select、Where、Count 和 Distinct

本文关键字:Where Count Distinct select 具有 LINQ Lambda 查询 | 更新日期: 2023-09-27 18:36:50

我是 linq 的新手,我需要查询方面的帮助。我在数据表中有 3 列。我需要从 col3 中获取唯一值的计数,其中 col1 和 Col2 包含某些值。这是我尝试的最后一段代码,但它不起作用。有人可以帮我解决这个问题吗?

谢谢

AD = dt.AsEnumerable()
    .Where(x => x.Field<string>("Col1").Equals("Value1") 
             || x.Field<string>("Col2").Equals("Value2"))
    .Select(s => s.Field<string>("Col3")
    .Distinct().Count());

C# LINQ Lambda 查询,具有 select、Where、Count 和 Distinct

我在.Select(s => s.Field<string>("Col3")缺少右括号,试试这个:

AD = dt.AsEnumerable()
    .Where(x => x.Field<string>("Col1").Equals("Value1") 
             || x.Field<string>("Col2").Equals("Value2"))
    .Select(s => s.Field<string>("Col3")) // <-- add this
    .Distinct().Count();   // <-- remove this
var q1  = (from persons in _db.Table<Person>()
            where persons.firstName==fname && persons.lastName==lname group
 persons.age by persons.age ).Count();

我已经使用 sqlite-net 库在一些"测试"案例中对此进行了测试——它似乎有效。虽然已经有一个解决方案 - 他们使用不同的样式,所以我决定把它发布在那里