我有兩個LINQ查詢如下:合併兩個查詢在LINQ
GroupNamesWithCorrespondingEffects
= new ObservableCollection<GroupNameWithCorrespondingEffect>(
from g in db.Groups
select new GroupNameWithCorrespondingEffect
{
GroupID = g.GroupID,
GroupName = g.GroupName,
CorrespondingEffect = g.Master_Effects.Effect
}
);
GroupNamesWithCorrespondingEffects
= new ObservableCollection<GroupNameWithCorrespondingEffect>
(GroupNamesWithCorrespondingEffects.
Where(u => !GetAllChildren(25).
Select(x => x.GroupID).
Contains(u.GroupID)).ToList());
現在,我怎麼合併這兩個查詢?
我已經試過你的答案以及尼爾的其他答案。在這兩種情況下,我得到以下錯誤:'LINQ to Entities無法識別'System.Collections.Generic.IEnumerable'1 [WPF_Client.HelperClasses.GroupNameWithCorrespondingEffect] GetAllChildren(Int32)'方法,並且此方法無法翻譯' – Vishal
好的,EF無法撰寫,因爲我懷疑。它應該像提取GetAllChildren(25)一樣簡單。在變量中選擇(x => x.GroupID).ToList()並使用該變量來執行Contains。如果那個引發同樣的問題,你可以拿Neil的答案,並在.Where調用之前添加一個ToList()。第二部分將在內存中運行,但它與您在代碼中所做的無關。 – Wasp
感謝您解釋這一行:'第二部分將在內存中運行,但它與您在代碼中所做的沒有什麼不同。「同時感謝您的回答。 – Vishal