简体   繁体   English

C#Linq .Find()返回许多结果

[英]C# Linq .Find() return many results

I am trying to create a simple search function for my application. 我正在尝试为我的应用程序创建一个简单的搜索功能。 I am using Linq's .Find() method to search through a list of objects. 我正在使用Linq的.Find()方法搜索对象列表。 It all works very well, the only problem I'm currently having is that I only get the first result. 一切都很好,我目前遇到的唯一问题是我只得到第一个结果。 I know for a fact that there are more than one results to be had, but I only get one. 我知道有一个以上的结果,但我只有一个。 Here is my code: 这是我的代码:

case 5: {
    //Search for Price

    Product searchResult = tempList.Find(x => x.getPrice() == searchPrice);
    if (searchResult != null) {
        //Present Result
        searchTable.Rows.Add(convertIDValue(searchResult.getProductID()), searchResult.getTitle(), searchResult.getYear(), searchResult.getAmount(), searchResult.getPrice());
    }
    else {
        MessageBox.Show("No product with that price", "0 results");
    }

    break;
}

I thought I could change Product searchResult into List<Product> searchResults in order to get a list of Products, then loop through that list. 我以为可以将Product searchResult更改为List<Product> searchResults以获取Products列表,然后遍历该列表。 But that gave me an error saying: 但这给了我一个错误:

Cannot implicitly convert type '.Product' to 'System.Collections.Generic.List<.Product> 无法将类型'.Product'隐式转换为'System.Collections.Generic.List <.Product>

Is there any way to get Linq's .Find() to return multiple results? 有什么方法可以使Linq的.Find()返回多个结果吗?

Use Where() and ToList() to get all objects, matching the condition into a List 使用Where()ToList()获取所有对象,将条件匹配到List

replace 更换

Product searchResult = tempList.Find(x => x.getPrice() == searchPrice);

with

List<Product> searchResult = tempList.Where(x => x.getPrice() == searchPrice).ToList();

为此有一个FindAll方法:

List<Product> products = tempList.FindAll(x => x.getPrice() == searchPrice);

Find() searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List. Find()搜索与指定谓词定义的条件匹配的元素,并返回整个List中的第一个匹配项。

You need to use FindAll() instead. 您需要使用FindAll()代替。

Microsoft explains the "Find()" method :"Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List." Microsoft解释了“ Find()”方法:“搜索与指定谓词定义的条件匹配的元素,并返回整个List中的第一个匹配项。”

I would suggest you to use this Where() method from Linq extension. 我建议您使用Linq扩展中的Where()方法

Don't forget to import "using System.Linq" in your current class. 不要忘记在当前类中导入 “ using System.Linq”。

Product searchResult = 

means you are declaring one element. 表示您要声明一个元素。 The thing you need is a collection of products, like: 您需要的是一系列产品,例如:

IEnumerable<product> searchResult  =

The easiest way to do it is to change Find() to where(): 最简单的方法是将Find()更改为where():

IEnumerable<product> searchResult = tempList.Where(x => x.getPrice() == searchPrice);

this will create some collection of product's. 这将创建一些产品集合。 It will be easier to maintain as a list, so: 列表将更易于维护,因此:

list<product> searchResult = tempList.Where(x => x.getPrice() == searchPrice).toList();

Read about IEnumerable interface :) 了解有关IEnumerable接口的信息:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM