0

使用 LINQ,我如何检索与另一个列表匹配的项目列表?

例子:

List<string> data = new List<string>();
List<List<string>> datas = new List<List<string>>();
List<string> dataSearch = new List<string>();

dataSearch.Add("01");
dataSearch.Add("02");

data.Add("01");
data.Add("Book");
data.Add("9");
datas.Add(data);

data = new List<string>();
data.Add("02");
data.Add("Pen");
data.Add("2");
datas.Add(data);

data = new List<string>();
data.Add("03");
data.Add("Pencil");
data.Add("5");
datas.Add(data);

预期结果List<List<string>>

List<string> ("01", "Book", "9")
List<string> ("02", "Pen", "2")

我试试

var result = datas.Where(x => x[0].Contains(dataSearch)).ToList();

我在编辑器中得到了红色下划线。

4

2 回答 2

3

这就是您要查找的内容:

List<string> data = new List<string>();
List<List<string>> datas = new List<List<string>>();
List<string> dataSearch = new List<string>();

dataSearch.Add("01");
dataSearch.Add("02");

// same as your code

var result = datas.Where(i => dataSearch.Contains(i[0])).ToList();
于 2013-10-04T10:59:12.730 回答
-1

如果 中的三个项目List<List<string>>具有紧密的关系,最好为此创建一个类,然后列出这些对象。

Class StationaryItem
         {
           int ItemCode;
           String ItemName;
           float Price;

         public StationaryItem(int Code, string ItemName,float Price)
            {
              this.ItemCode = Code;
              this.ItemName = ItemName;
              this.Price = Price;
            }
         }

public static void Main ()
      {
         List<StationaryItem> ItemList = new List<StationaryItems>
         ItemList.Add( new StationaryItem(01,"Pen",20));
         ItemList.Add( new StationaryItem(02,"Pencil",5));

         //After you make a class like this you can use Linq to manuplate easily
      }
于 2013-10-04T11:06:17.937 回答