簡體   English   中英

從列表中獲取結果 <T> C#

[英]Getting results from a List<T> C#

我試圖在對象列表中查找原始類型的代碼,並將結果放入新列表中。

我一直在Google中尋找類似的東西,但是每個人都在談論List之間的交集,我想要這樣的東西。

            int AssocCode = 2;
            //Entity.ID
            //Entity.Name
            //Entity.AssociationCode
            List<Entity> list = new List<Entity>();
            list.Add(new Entity(1, "A", 1));
            list.Add(new Entity(2, "B", 2));
            list.Add(new Entity(3, "C", 3));
            list.Add(new Entity(4, "D", 2));
            list.Add(new Entity(5, "E", 2));

            /*I want the results for finding that code inside the collection**/
            List<Entity> newList = list .... find/intersect/select/remove/whatever (x => x.AssociationCode = AssocCode);
int associationCode = 1;

List<Entity> newList = list.Where(e => e.AssociationCode == associationCode).ToList();

值得一提的是,僅當您需要結果時才應調用ToList()

如果您不需要立即獲得結果,這會更好。

IQueryable<Entity> entities = list.Where(e => e.AssociationCode == associationCode);

然后,您可以進一步過濾而無需訪問數據庫。

應該這樣做:

var newList = list.Where(e => e.AssociationCode == AssocCode).ToList()

1分鍾前我解決了我的問題。 就是這樣。

            List<Entity> newList =list.FindAll(item => item.AssociationCode == associationCode)

但是現在我看到了您的答案PeteGO,並且進行了測試,它也可以工作,但最后您是新添加此.ToList()的人

現在我有點困惑。 這兩個有什么區別

            fullList = this.FullList.FindAll(item => item.AccCompanyID == CompanyCode);
            vs
            fullList = this.FullList.Where(item => item.AccCompanyID == CompanyCode).ToList();

暫無
暫無

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

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