簡體   English   中英

具有選擇,位置,計數和區別的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")缺少.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庫在某些“測試”用例上對此進行了測試-似乎可行。 盡管已經有了解決方案-他們使用的是不同的樣式,所以我決定將其發布到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM