[問題] 透過Lambda過濾資料...

看板C_Sharp (C#)作者 (1+1≠2)時間8年前 (2016/04/15 23:08), 編輯推噓15(15040)
留言55則, 13人參與, 最新討論串1/1
我定義一個物件,裡面有以下資訊... public class Person { public string Name; public int Age; public string Sex; }; 我使用一個List容器來裝此物件... List<Person> PerList = new List<Person>(){ new Person(){ Name = "Sam", Age = 50, Sex = "Man" }, new Person(){ Name = "Amy", Age = 30, Sex = "Woman"}, new Person(){ Name = "Andy", Age = 30, Sex = "Man"}, new Person(){ Name = "Jacky", Age = 60, Sex = "Man"}, new Person(){ Name = "Peter", Age = 65, Sex = "Man"}, new Person(){ Name = "Susan", Age = 45, Sex = "Woman"}, new Person(){ Name = "Jeff", Age = 65, Sex = "Man"}, new Person(){ Name = "Mary", Age = 63, Sex = "Woman" }; 假設,我有一組資料如上,而我希望可以直接過濾掉其他不需要的資料。 我希望可以"保留" Age 前三名最大的... 也就是說,剩下Mary,Jacky,Peter,Jeff... 想請問一下,如果透過Lambda或是LINQ有辦法過濾出前三名Age最大的嗎?? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.142.153.51 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1460732888.A.F48.html

04/15 23:21, , 1F
var v=PerList.OrderByDescending(x => x.Age).Take(3)
04/15 23:21, 1F

04/15 23:31, , 2F
照樓上大大的做法,只會抓出排序後的前三筆吧?..
04/15 23:31, 2F

04/15 23:32, , 3F
我要抓出的結果應該會有4筆,65.65.63.60..
04/15 23:32, 3F

04/15 23:36, , 4F
先抓第三名的值再用這個值抓出大於等於此值的?
04/15 23:36, 4F

04/15 23:46, , 5F
var v = PerList.Where(x => x.Age >=
04/15 23:46, 5F

04/15 23:46, , 6F
var thirdNumber = perList.Select(o => o.Age).Distinct().
04/15 23:46, 6F

04/15 23:46, , 7F
PerList.GroupBy(y => y.Age).ToList()[2].Key)
04/15 23:46, 7F

04/15 23:46, , 8F
OrderByDescending(o => o).Skip(2).First();
04/15 23:46, 8F

04/15 23:46, , 9F
.OrderByDescending(z => z.Age);
04/15 23:46, 9F

04/15 23:46, , 10F
var result = perList.Where(p => p.Age >= thirdNumber);
04/15 23:46, 10F

04/15 23:47, , 11F
啊 不好意思 斷到樓上iphone大的推文了 囧
04/15 23:47, 11F

04/15 23:50, , 12F
04/15 23:50, 12F

04/16 03:49, , 13F
兩位你們這樣年齡種類不夠多會跳錯,不過很接近答案了
04/16 03:49, 13F

04/16 03:52, , 14F
但我不想回答原PO問題,所以...晚安 ( ゜∀。)
04/16 03:52, 14F

04/16 07:07, , 15F
樓上Lit前輩說到重點了@@,如果年齡種類只有兩種...
04/16 07:07, 15F

04/16 07:07, , 16F
當然就是全抓了....
04/16 07:07, 16F

04/16 11:36, , 17F
var desc = PerList.OrderByDescending(q => q.Age);
04/16 11:36, 17F

04/16 11:36, , 18F
var topAges = desc.GroupBy(q => q.Age).Select(q=>q.Ke
04/16 11:36, 18F

04/16 11:36, , 19F
y).Take(3);
04/16 11:36, 19F

04/16 11:37, , 20F
PerList = desc.Where(q => topAges.Contains(q.Age)).To
04/16 11:37, 20F

04/16 11:37, , 21F
List();
04/16 11:37, 21F

04/16 11:38, , 22F
desc也用ToList()會比較..
04/16 11:38, 22F

04/16 11:38, , 23F
比較好@@
04/16 11:38, 23F

04/16 11:39, , 24F
topAges也是
04/16 11:39, 24F

04/16 11:58, , 25F
參考上面大大,對topAges取LastOrDefault,再比大小會更好
04/16 11:58, 25F

04/16 13:58, , 26F
自己有想過,做兩次處理,第一次先取出前三個年紀
04/16 13:58, 26F

04/16 13:59, , 27F
第二次再將符合前三的挑出來...
04/16 13:59, 27F

04/16 13:59, , 28F
會想問能不能透過Lambda,是想說能不能一次處理...
04/16 13:59, 28F

04/16 13:59, , 29F
看起來還是要分兩次處理比較好
04/16 13:59, 29F

04/16 14:00, , 30F
謝謝各位前輩
04/16 14:00, 30F

04/16 14:11, , 31F
提示一下:你們都做到group by了,為什麼還要做where呢?
04/16 14:11, 31F

04/16 21:13, , 32F
推L大XD~ 沒想到這個做法 GroupBy與Take(3)後判斷長度對
04/16 21:13, 32F

04/16 21:13, , 33F
結果列舉就可以得到前三大了
04/16 21:13, 33F

04/16 21:38, , 34F
比大小的作法用Distinct則可以省略GroupBy的動作
04/16 21:38, 34F

04/16 23:03, , 35F
可惜!還是差一點點,Take(3)沒錯,但之前之後要做一些事
04/16 23:03, 35F

04/17 00:48, , 36F
L大是指直接用Aggregate合併嗎?
04/17 00:48, 36F

04/17 00:50, , 37F
PerList.OrderByDescending(q => q.Age).
04/17 00:50, 37F

04/17 00:50, , 38F
GroupBy(q => q.Age).Take(3).Select(q => q.ToList()).
04/17 00:50, 38F

04/17 00:51, , 39F
Aggregate(new List<Person>(), (list, current) =>
04/17 00:51, 39F

04/17 00:51, , 40F
{ list.AddRange(current); return list; });
04/17 00:51, 40F

04/17 00:54, , 41F
弄個.net fiddle來討論不是很好嗎0.0...
04/17 00:54, 41F

04/17 01:01, , 42F
XD 學習不同方法很有趣
04/17 01:01, 42F

04/18 23:11, , 43F
雖然原PO會問完問題放置,讓我不太想回...不過版友討論成
04/18 23:11, 43F

04/18 23:12, , 44F
這樣卻只差答案一點點感覺有點可惜
04/18 23:12, 44F

04/18 23:12, , 45F

04/19 01:15, , 46F
原來有SelectMany可以用,學習了!
04/19 01:15, 46F

04/19 21:20, , 47F
有學到了!謝謝!
04/19 21:20, 47F

04/19 21:59, , 48F
不用SelectMany也可以用雙層迴圈解決
04/19 21:59, 48F

04/19 21:59, , 49F

04/20 15:45, , 50F
有下有推
04/20 15:45, 50F

04/26 14:23, , 51F
有學有推<(_ _)>
04/26 14:23, 51F

04/27 09:11, , 52F
這篇M一下嘛
04/27 09:11, 52F

05/17 16:04, , 53F
推一下....
05/17 16:04, 53F

05/24 13:44, , 54F
有看有學 >//<
05/24 13:44, 54F

09/26 15:23, , 55F
...有點神
09/26 15:23, 55F
文章代碼(AID): #1N4GFOz8 (C_Sharp)
文章代碼(AID): #1N4GFOz8 (C_Sharp)