具有下面的例子:LINQ到實體 - SQL查詢 - 如果列表中包含的對象有2個屬性(或更多)
var myIds = db.Table1.Where(x=>x.Prop2 == myFilter).Select(x=>x.Id).ToList();
var results = db.Table2.Where(x=> myIds.Contains(x.T1)).ToList();
這部分是直線前進。
但是,我現在面對的在我的「篩選器列表」有2個屬性,而不是隻有一個「輕微」的變化:
// NOTE: for stackoverflow simplification I use a basic query to
// get my "myCombinationObject".
// In reality this is a much more complex case,
// but the end result is a LIST of objects with two properties.
var myCombinationObject = db.Table3.Where(x=>x.Prop3 == myFilter)
.Select(x=> new {
Id1 = x.T1,
Id2 = x.T2
}).ToList();
var myCombinationObjectId1s = myCombinationObject.Select(x=>xId1).ToList();
var myCombinationObjectId2s = myCombinationObject.Select(x=>xId2).ToList();
// step#1 - DB SQL part
var resultsRaw = db.Tables.Where(x=>
myCombinationObjectId1s.Contains(x.Prop1)
|| myCombinationObjectId2s.Contains(x.Prop2))
.ToList();
// step#2 - Now in memory side - where I make the final combination filter.
var resultsFiltered = resultsRaw.Where(x=>
myCombinationObject.Contains(
new {Id1 = x.Prop1, Id2 = x.Prop2 }
).ToList();
我的問題:是它甚至有可能合併的步驟# 2在步驟#1(在LINQ查詢實體)?
不幸的是,它是複雜的問題。請閱讀我的問題 - 或許真的將是有益的:http://stackoverflow.com/questions/25563338/linq-simulating-multiple-columns-in-in-clausule –