A
Andy
Hi, I'm trying to replicate the following query using L2S:
select *
from citation c
inner join citationkeyword kc on c.citationid = kc.citationid
inner join keyword k on kc.keywordid = k.keywordid
where (
k.Keyword like @p0
OR k.Keyword like @p1
OR k.Keyword like @p2
...
OR k.Keyword like @pX
)
And of course there are a variable number of Like clauses. I've tried
this, but I get too many results:
CitationsDataContext
dc;
IQueryable<Citation> result;
IEnumerable<KeywordList.NameValuePair> keyResults;
string keyword;
KeywordList keywordList;
using ( mgr = ContextManager<MedCitationsDataContext>.GetManager
() ) {
keywordList = from kwords in dc.Keyword
keyword = keywords[ 0 ];
keyResults =
from kwords in keywordList
where kwords.Value.Contains( keyword )
select kwords;
for ( int i = 1 ; i < keywords.Length ; i += 1 ) {
keyword = keywords[ i ];
keyResults = keyResults.Union(
from kwords in keywordList
where kwords.Value.Contains( keyword )
select kwords
);
}
var keyIds =
from kr in keyResults
select kr.Key;
result =
from citations in mgr.DataContext.Citation
join citationKeywords in mgr.DataContext.CitationKeyword
on citations.CitationId equals citationKeywords.CitationId
where keyIds.Contains( citationKeywords.KeywordId )
select citations;
Instead, I get all rows in the Citation table returned. Any ideas?
select *
from citation c
inner join citationkeyword kc on c.citationid = kc.citationid
inner join keyword k on kc.keywordid = k.keywordid
where (
k.Keyword like @p0
OR k.Keyword like @p1
OR k.Keyword like @p2
...
OR k.Keyword like @pX
)
And of course there are a variable number of Like clauses. I've tried
this, but I get too many results:
CitationsDataContext
dc;
IQueryable<Citation> result;
IEnumerable<KeywordList.NameValuePair> keyResults;
string keyword;
KeywordList keywordList;
using ( mgr = ContextManager<MedCitationsDataContext>.GetManager
() ) {
keywordList = from kwords in dc.Keyword
keyword = keywords[ 0 ];
keyResults =
from kwords in keywordList
where kwords.Value.Contains( keyword )
select kwords;
for ( int i = 1 ; i < keywords.Length ; i += 1 ) {
keyword = keywords[ i ];
keyResults = keyResults.Union(
from kwords in keywordList
where kwords.Value.Contains( keyword )
select kwords
);
}
var keyIds =
from kr in keyResults
select kr.Key;
result =
from citations in mgr.DataContext.Citation
join citationKeywords in mgr.DataContext.CitationKeyword
on citations.CitationId equals citationKeywords.CitationId
where keyIds.Contains( citationKeywords.KeywordId )
select citations;
Instead, I get all rows in the Citation table returned. Any ideas?