gpt4 book ai didi

c# - 用于根据另一个列表从一个列表中进行选择的 linq 查询

转载 作者:太空狗 更新时间:2023-10-29 22:33:58 25 4
gpt4 key购买 nike

public class Test
{
int i;
string s;
}

List<Test> testList = new List<Test>(); //assume there are some values in it.

List<int> intList = new List<int>(){ 1,2,3};

我怎么说得到items from testList where i is in intList使用 linq to objects。

类似于List<Test> testIntList = testList.Where(t=>t.i in intList)

最佳答案

从技术上讲,它将是:

List<Test> testIntList = testList.Where(t => intList.Contains(t.i)).ToList();

但是,如果intList,那可能会很慢很大,因为 List<T>.Contains在 O(n) 中执行搜索。更快的方法是使用 HashSet<T> :

HashSet<int> intList = new HashSet<int>(){ 1,2,3 };

关于c# - 用于根据另一个列表从一个列表中进行选择的 linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com