简体   繁体   English

在类型类 c# 的列表中搜索

[英]Search within a list of type class c#

I have a class which has an ID and a few columns, one of which is a string.我有一个类,它有一个 ID 和几列,其中一个是字符串。

public class myData
    {
        public Int64 myID { get; set; }
        public string myTitle{ get; set; }
        public string myForename{ get; set; }
        public string mySurname{ get; set; }
    }

There are a few million rows in the list and I want to search for all rows that have a mySurname that contains a specific string列表中有几百万行,我想搜索具有包含特定字符串的 mySurname 的所有行

I started to look at Contains but a little stuck on syntax where searching within a class, all examples I can find are only searching in a simple list of type string.我开始查看 contains 但有点停留在在类中搜索的语法上,我能找到的所有示例都只在一个简单的字符串类型列表中搜索。

Ideally I want to get the full rows that match, or if not then the ID will do fine理想情况下,我想获得匹配的完整行,否则 ID 会很好

Consider you have the following list:考虑您有以下列表:

List<myData> dta = new List<myData>()
{
    new myData()
    {
        myID = 1,
        mySurname = "tst"
    },
    new myData()
    {
        myID = 1,
        mySurname = "11test2"
    }
};

If you need to return in a list all the records that contain the word 'test' in the mySurname field, you can do it with 2 ways that are exactly the same:如果您需要在列表中返回mySurname字段中包含单词“test”的所有记录,您可以使用 2 种完全相同的方法来完成:

1) 1)

var res = dta.Where(d => d.mySurname.Contains("test")).ToList();
var res2 = (from d in dta
            where d.mySurname.Contains("test")
            select d).ToList();

You can use the LINQ您可以使用 LINQ

List<myData> list = new List<myData>();
myData m = list.Find(s=>s.mySurname==specificString);

Since you mentioned the arraylist tag, you can write a query like:由于您提到了arraylist标记,您可以编写如下查询:

var data = new ArrayList();

data.Add(new myData { mySurname = "john 1" });
data.Add(new myData { mySurname = "john 2" });
data.Add(new myData { mySurname = "peter 1" });
data.Add(new myData { mySurname = "peter 2" });

var result = from myData item in data
             where item.mySurname.Contains("peter")
             select item;

foreach ( var item in result )
  Console.WriteLine(item.mySurname);

To have a List<myData> typed collection, you can also write:要拥有List<myData>类型的集合,您还可以编写:

var list = result.ToList()

Or if you need an ArrayList:或者,如果您需要一个 ArrayList:

var dataFiltered = new ArrayList(result.ToArray());

foreach ( myData item in dataFiltered )

If you need to do a case insensitive search:如果您需要进行不区分大小写的搜索:

Case insensitive 'Contains(string)' 不区分大小写的“包含(字符串)”

You should use a List<myData> instead of an ArrayList :您应该使用List<myData>而不是ArrayList

c# When should I use List and when should I use arraylist? c# 什么时候应该使用List,什么时候应该使用arraylist?

https://docs.microsoft.com/dotnet/api/system.collections.arraylist https://docs.microsoft.com/dotnet/api/system.collections.arraylist

Output输出

peter 1
peter 2

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

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