gpt4 book ai didi

c# - 减少类似查询中的重复

转载 作者:行者123 更新时间:2023-11-30 21:00:54 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How can I send where statements to a method which are dynamically executed in a LINQ statement?

我一直在用不同的方法编写简单的 LINQ 查询,但它们变得重复。请原谅名称和地点的愚蠢示例,所有三种方法中唯一改变的部分是“where”子句。

如何让这段重复的代码变得更多dry

例如,类似于辅助方法的东西,它采用相同的所有内容但允许更改 where 子句。

public IEnumerable<NamePlace> GetSomeData(int num1, int num2)
{
var temp = from Name in Names
from Place in Places
where Name.id == num1 && Place.id = num2
select new NamePlace {
field1 = Name.name;
field2 = Place.name;
};
return temp;
}

public IEnumerable<NamePlace> GetSomeData2(int num1, int num2)
{
var temp = from Name in Names
from Place in Places
where Name.age == num1 && Place.streetNumber = num2
select new NamePlace {
field1 = Name.name;
field2 = Place.name;
};
return temp;
}

public IEnumerable<NamePlace> GetSomeData3(int num1, int num2)
{
var temp = from Name in Names
from Place in Places
where Name.favouriteNumber == num1 && Place.neighbourNumber = num2
select new NamePlace {
field1 = Name.name;
field2 = Place.name;
};
return temp;
}

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