C#将lambda表达式字段传递给linq查询中的方法和使用字段

时间:2016-09-01 15:18:07

标签: c# linq lambda parameters

如何将字段(通过lambda表达式)传递给方法,然后将该字段用作linq查询的一部分?

我想把方法称为

IDictionary<string, string> stuff = foo(items, otherItems, otherItems => otherItems.FieldToUse)

我不确定如何编写该方法,但我想使用它类似于下面的代码。我知道我可以使用泛型并将字段名称(通过字符串)传递给方法,但即便如此,我也不知道如何在linq查询中使用它,如下所示。另外,我喜欢使用lambda,因为我可以随时重命名该字段。

private IDictionary<string, string> foo<TModel>(IEnumerable<string> items, IEnumerable<TModel> otherItems, object FieldToUse)
    {
        //this will return a list of key value pairs of rowIDs and equipment
        IDictionary<string, string> x = (from o in otherItems
                                         join i in items on o.FieldToUse.ToString() equals i //joining on the equipment assetcode
                                         select new { rowID = o.RowID, item = i }).ToDictionary(k => k.rowID.ToString(), v => v.item);
        return x;
    }

澄清:FieldToUse是TModel的属性或字段

1 个答案:

答案 0 :(得分:3)

使用Func Delegate

将方法foo中的最后一个参数更改为

Func<TModel, String> FieldToUse

并在LINQ查询中调用函数

FieldToUse(o)

这是整个方法foo

private IDictionary<string, string> foo<TModel>(IEnumerable<string> items,
  IEnumerable<TModel> otherItems,
  Func<TModel, String> FieldToUse)
{
  //this will return a list of key value pairs of rowIDs and equipment
  IDictionary<string, string> x = (from o in otherItems
                                   join i in items on FieldToUse(o) equals i //joining on the equipment assetcode
                                   select new { rowID = o.RowID, item = i })
                                   .ToDictionary(k => k.rowID.ToString(), v => v.item);
  return x;
}

这就是你如何使用它

public void DoStuff()
{
  string[] items = { "abc", "def", "ghi" };
  List<Model> otherItems = new List<Model> { 
        new Model() { Field1 = "abc", Field2 = "xyz" }, 
        new Model() { Field1 = "abc", Field2 = "xyz" } };

  var result = foo<Model>(items, otherItems, a => a.Field2);
}

class Model 
{
  public string Field1 { get; set; }
  public string Field2 { get; set; }
}

但是你会遇到另一个问题。通用TModel没有RowID。也许为TModel提供generic where constraint

然后代码变为

 private IDictionary<string, string> foo<TModel>(IEnumerable<string> items,
  IEnumerable<TModel> otherItems,
  Func<TModel, String> FieldToUse) where TModel : BaseModel
{
  //this will return a list of key value pairs of rowIDs and equipment
  IDictionary<string, string> x = (from o in otherItems
                                   join i in items on FieldToUse(o) equals i //joining on the equipment assetcode
                                   select new { rowID = o.RowID, item = i })
                                   .ToDictionary(k => k.rowID.ToString(), v => v.item);
  return x;
}

class BaseModel
{
  public int RowID { get; set; }
}
class Model : BaseModel
{
  public string Field1 { get; set; }
  public string Field2 { get; set; }
}