[英]Can not select many items from list using LINQ or lambda query
我有靜態類中的對象列表。 像這樣:
public static class ClassOfItems
{
private static List<Item> ListOfItems = new List<Item>();
public static List<Item> GetAll()
{
return ListOfItems;
}
}
public class Item
{
public int id{get; set;}
public string name{get;set:}
}
我從XML文件初始化項目。 直到那一刻,一切都OK。 例如,我的靜態類中有兩個項目:
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0); Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
item1: id=1 name="item1" item2: id=2 name="item2"
Now I want to select items where id value is more then 0 (for example):
1 way: var items=from i in ClassOfItems.GetAll() where i.id>0 select i;
2 way: var items=ClassOfItems.GetAll().Where(i => i.id>0);
Both of them not working :( I just noticed that when I use methods with returns single objects it's working. For example: var items=ClassOfItems.GetAll().First(i => i==1);//it's working var items=ClassOfItems.GetAll().Where(i=> i==1);//not working ;(
haha solved sorry guys :D just needed to use ToList() method.
var items=(ClassOfItems.GetAll().Where(i => i.id>0)).ToList();
Select()
轉換對象。 因此,在最后一個Select語句中,您將返回bool
如果期望一個結果,則使用First(i=> i.id==1)
或FirstOrDefault(i=> i.id==1)
如果您期望一個結果,則使用Where(i=> i.id==1)
期待多種結果。
如果查看文檔,則將看到Select
和Where
方法返回IEnumerable<TResult>
,即使您的序列只有一個元素也沒關系。
實際上,您的Where方法按預期工作。如果您在Where之后看到foreach
循環遍歷您的列表,那我是正確的。使用yield return的可枚舉擴展方法,這就是為什么在調試器中看不到列表的原因。 WhereListIterator.
您可以查看此文檔以獲取有關yield關鍵字的更多詳細信息。
您可以通過使用foreach語句或LINQ查詢來使用迭代器方法。 foreach循環的每次迭代都調用iterator方法。 在迭代器方法中到達yield return語句時,將返回expression,並保留代碼中的當前位置。 下次調用迭代器函數時,將從該位置重新開始執行。
但是,如果要獲取列表,則在以下位置使用ToList
方法:
var items=ClassOfItems.GetAll().Where(i=> i==1).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.