2016-09-01 3 views
0

メソッドにLambda式を使用してフィールドを渡して、そのフィールドをlinqクエリの一部として使用するにはどうすればよいですか?C#ラムダ式フィールドをメソッドに渡し、linqクエリのフィールドを使用する

私は

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

のような方法何かを呼び出すしたいと思い、私はメソッドを書くだろうかわからないんだけど、私は一種の下記のコードのようにそれを使用したいと思います。私はジェネリックスを使用し、フィールド名(文字列を介して)をメソッドに渡すことができますが、その場合でも、linqクエリでどのように使用するのかはわかりません。また、私はラムダを使うのが好きです。なぜなら、いつでも自分が選んだフィールドの名前を変更できるからです。

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ののプロパティまたはフィールドである

+0

質問は非常に明確ではありませんになります。 FieldsToUseはotherItemsの型ですか? otherItemsを渡しているときは、フィールドを渡す必要はありません。 –

+0

TModelに同じ型を使用していても、同じプロパティを使用してメソッドを常に呼び出す必要はありません。時には1つのプロパティと別のプロパティを使用したい場合があります – TruthOf42

+0

セレクタを使いたいと思います。例として、.OrderBy(obj => obj.Property)を呼び出すLINQ OrderByメソッドがあります。それはあなたが探しているものですか? – mbrdev

答えて

3

使用Func Delegate

変更呼ん

Func<TModel, String> FieldToUse 

へとLINQクエリでメソッドfooで最後のパラメータ機能

FieldToUse(o) 
ここ

これはあなたがあなたがが、別の問題がありますそれを

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; } 
} 

を使用する方法である全体のメソッド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; 
} 

です。ジェネリック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; } 
} 
関連する問題