Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 减少类似查询中的重复_C#_Linq - Fatal编程技术网

C# 减少类似查询中的重复

C# 减少类似查询中的重复,c#,linq,C#,Linq,可能重复: 我一直在用不同的方法编写简单的LINQ查询,但它们已经变得重复。请原谅这个愚蠢的名称和地点的例子,这三种方法中唯一改变的部分是where子句 如何使这些重复的代码变得更加复杂 例如,类似于helper方法的东西,它接受所有相同的内容,但允许在where子句中进行更改 public IEnumerable<NamePlace> GetSomeData(int num1, int num2) { var temp = from Name in Names

可能重复:

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

如何使这些重复的代码变得更加复杂

例如,类似于helper方法的东西,它接受所有相同的内容,但允许在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;
}
public IEnumerable<NamePlace> GetSomeData(Func<Name,Place, bool> condition)
{
  var temp = from Name in Names
             from Place in Places
             where condition(Name,Place)
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}
将Func传递给方法,在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;
}
public IEnumerable<NamePlace> GetSomeData(Func<Name,Place, bool> condition)
{
  var temp = from Name in Names
             from Place in Places
             where condition(Name,Place)
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}
将Func传递给方法,在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;
}
public IEnumerable<NamePlace> GetSomeData(Func<Name,Place, bool> condition)
{
  var temp = from Name in Names
             from Place in Places
             where condition(Name,Place)
             select new NamePlace {
                  field1 = Name.name;
                  field2 = Place.name;
             };
  return temp;
}

可以使GetSomeData接受谓词:

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

可以使GetSomeData接受谓词:

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

您只需要一个Func类型的参数,其中T1和T2分别是名称和位置项的类型

private IEnumerable<NamePlace> GetSomeDataHelper(Func<Name, Place, bool> filter)
{
  var query = from name in Names
             from place in Places
             where filter(name, place)
             select new NamePlace {
                  field1 = name.name,
                  field2 = place.name,
             };
  return query;
}

public IEnumerable<NamePlace> GetSomeData1(int num1, int num2)
{
    return GetSomeDataHelper((name, place) => name.id == num1 && place.id = num2);
}

您只需要一个Func类型的参数,其中T1和T2分别是名称和位置项的类型

private IEnumerable<NamePlace> GetSomeDataHelper(Func<Name, Place, bool> filter)
{
  var query = from name in Names
             from place in Places
             where filter(name, place)
             select new NamePlace {
                  field1 = name.name,
                  field2 = place.name,
             };
  return query;
}

public IEnumerable<NamePlace> GetSomeData1(int num1, int num2)
{
    return GetSomeDataHelper((name, place) => name.id == num1 && place.id = num2);
}

为什么不使用或语句为什么不使用或语句刚要发布几乎相同的代码:正准备发布几乎相同的代码: