使用 select、Where、Count 和 Distinct 的 C# LINQ Lambda 查询

C# LINQ Lambda query with select, Where, Count and Distinct

我是 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());

我在 .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 库在某些 "test" 案例中对此进行了测试 - 它似乎有效。 虽然已经有解决方案 - 他们使用不同的样式,所以我决定 post 到