如何选择多个ID?

时间:2018-12-12 12:37:27

标签: c# .net linq

有一个列表List<string> list

如何使用LINQ构建查询,该查询重复查询"Select * FROM a Where id IN(...)"

2 个答案:

答案 0 :(得分:4)

使用LINQContains方法:

var result = a.Where(c => list.Contains(c.Id)).ToList();

答案 1 :(得分:1)

您也可以使用public csvNewContent: string; onFileLoad(fileLoadedEvent) { let csvContent = fileLoadedEvent.target.result; let re = /;/gi; this.csvNewContent = csvContent.toString().replace(re,","); console.log(this.csvNewContent); } // This log prints the right value's onFileSelect(input: HTMLInputElement) { const files = input.files; if (files && files.length) { console.log("Filename: " + files[0].name); console.log("Type: " + files[0].type); console.log("Size: " + files[0].size + " bytes"); const fileToRead = files[0]; const fileReader = new FileReader(); fileReader.onload = this.onFileLoad; console.log(this.onFileLoad); fileReader.readAsText(fileToRead, "UTF-8"); } }

Any

如果您仅使用列表检查给定元素是否存在,建议您使用var result = source.Where(item => list.Any(element => element == item.Id)).ToList(); ,因为这样可能会获得更好的性能:

HashSet<string>

然后使用如下:

HashSet<string> container = ....