2013-06-26 61 views
0

我需要在LINQ模仿這對實體:如何構建以下動態LINQ to Entities WHERE子句?

WHERE 
(title = 'title1' AND episodetitle = 'episodetitle1') 
OR 
(title = 'title2' AND episodetitle = 'episodetitle2') 

我嘗試這樣做:

if (title1 != null && episodeTitle1 != null) 
{ 
     query = query.Where(s => s.Title.Contains(title1)).Where(z => z.EpisodeTitle.Contains(episodeTitle1)); 
} 

if (title2 != null && episodeTitle2 != null) 
{ 
     query = query.Where(s => s.Title.Contains(title2)).Where(z => z.EpisodeTitle.Contains(episodeTitle2)); 
} 

但它給我這個:

([Extent1].[Title] LIKE @p__linq__0 ESCAPE '~') AND ([Extent1].[EpisodeTitle] LIKE @p__linq__1 ESCAPE '~') 
**AND** 
([Extent1].[Title] LIKE @p__linq__2 ESCAPE '~') AND ([Extent1].[EpisodeTitle] LIKE @p__linq__3 ESCAPE '~')} 

我需要改變AND上面的OR

我該怎麼做呢?

回答

2

它串聯在一個查詢

query.Where(
s => (
(title1!=null && episodeTitle1!=null && s.Title.Contains(title1) && s.EpisodeTitle.Contains(episodeTitle1)) 
|| 
(title2!=null && episodeTitle2!=null && s .Title.Contains(title2) && s.EpisodeTitle.Contains(episodeTitle2)) 
+0

該訣竅,謝謝夥計! –

+0

很樂意幫忙=) –