在 sql 列表中搜索<int>?



有一个List类型的数组。此数组中有ID。我想用这些ID记录所有记录。我按如下操作。用";对于";,我总是在数组中旋转时进行或连接。就像(id=1或id=2或..(。有使用linq的快捷方式吗?非常感谢。

有一个Contains方法。你试过这个吗:

var listOfIds = new List<int> { 1, 23, 42 };
result = result.Where(r => listOfIds.Contains(r.ID)

请参阅.net Fiddlehttps://dotnetfiddle.net/zLlNeb

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

public class Program
{
public static void Main()
{
//if  you want from  list of strings
var listOfIds = new List<int> { 1, 23, 42 };
int id  = 23;
var result = listOfIds.AsEnumerable().Where(r => r == id).ToList();
Console.WriteLine(string.Join(",", result));

//---------------------------------------------------------------------

// if you want from a list of objects
List<Customer>  custList  = new List<Customer>();
custList.Add(new Customer("IBM","1001","19056"));
custList.Add(new Customer("IBM","1001","19056"));
custList.Add(new Customer("Microsoft","2002","65009"));
custList.Add(new Customer("Oracle","3003","5211"));

string custId  = "1001";
var result2 = custList.Where(r => r.Id == custId).ToList();
foreach(Customer c  in result2)
{
Console.WriteLine(string.Concat(c.Id,",",c.Name,",",c.Zip) );
}
}

}

public class Customer
{
public string Name {get; set;}
public string Id {get; set;}
public string Zip {get; set;}
public Customer(string name,string id, string zip)
{
this.Name = name;
this.Id  = id;
this.Zip  = zip;
}

}

最新更新