2015-01-07 254 views
0

我是linq的新手,我需要查詢幫助。我有一個數據表中的3列。我需要從col3中獲得唯一值的計數,其中col1和Col2包含某些值。這是我嘗試的最後一段代碼,但它不起作用。有人可以幫我這個嗎?C#LINQ Lambda query with select,Where,Count and Distinct

感謝

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

回答

2

我的思念在.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 
+0

這工作。謝謝! – user2219930

+0

@ user2219930:請注意,您的代碼編譯但做了一些不同的事情:它選擇了字符串字段'col3' ;-)中不同字符的數量 –

0
var q1 = (from persons in _db.Table<Person>() 
      where persons.firstName==fname && persons.lastName==lname group 
persons.age by persons.age).Count(); 

我已經在使用SQLite網一些 「測試」 的情況下測試了這個圖書館 - 它似乎工作。 雖然已經有一個解決方案 - 他們使用不同的風格,所以我決定發佈它到