2010-12-14 8 views
2

INに相当するラムダはありますか?私はどちらかのIDを持つすべての資金を選択するようになることを書いている4、5または6つの方法は次のとおりです。SQLに相当するLINQ&Lambda式In

リストfundHistoricalPrices = lionContext.FundHistoricalPrices.Where(FHP => fhp.Fund.FundId == 5 || fhp.Fund.FundId == 6 || fhp.Fund.FundId == 7).ToList();

しかし、100種類の異なる資金と一致させる必要がある場合は、すぐに管理できなくなります。 = lionContext.FundHistoricalPrices.Where(FHP => fhp.Fund.FundId中(5,6,7))

一覧 fundHistoricalPricesをToListメソッド();:私のような何かを行うことができます。

答えて

0

コレクションでContains()メソッドを使用すると、inに相当する値を取得できます。

var fundIds = new [] { 5, 6, 7 }; 

var fundHistoricalPrices = lionContext.FundHistoricalPrices.Where(fhp => fundIds.Contains(fhp.Fund.FundId)).ToList(); 
1

これはこれらの行のどこかにありますが、あなたが取ったアプローチには賛成できません。

.Where(fhp => new List<int>{5,6,7}.Contains(fhp.Fund.FundId)).ToList(); 

あなたのLINQクエリの前にIDのリストを構築したい場合があります。このような拡張メソッドを書くことができ...

0

を:あなたは本当にこれをしたい場合はしかし、これは行います

をその後
public static bool In<T>(this T source, params T[] list) 
{ 
    if(null==source) throw new ArgumentNullException("source"); 
    return list.Contains(source); 
} 

List fundHistoricalPrices = lionContext.FundHistoricalPrices.Where(fhp => fhp.Fund.FundId.In(5,6,7)).ToList(); 
0

ありませんが、私の知る唯一の同様のオペレータが含まれている()functio n。 http://www.albahari.com/nutshell/predicatebuilder.aspx

int[] fundIds = new int[] { 5,6,7}; 

var predicate = PredicateBuilder.False<FundHistoricalPrice>(); 

foreach (int id in fundIds) 
{ 
    int tmp = id; 
    predicate = predicate.Or (fhp => fhp.Fund.FundId == tmp); 
} 

var query = lionContext.FundHistoricalPrices.Where (predicate); 

他はLINQkitのうち、述語ビルダーを使って動的にクエリを構築することであるでした