tags:

views:

21

answers:

1

I have a fairly complex query where I am filtering results with a LIKE statement. Here is the query:

var qsFilter = entities.QueryStatements.Where("it.Statement LIKE @searchTerm", 
    new ObjectParameter("searchTerm", searchTerm));
var qtFilter = entities.QueryTables.Where("it.TableNames LIKE @searchTables", 
    new ObjectParameter("searchTables", searchTerm));

And then:

var tables = from t in entities.TraceLines
    join qs in qsFilter on t.QueryStatementHash equals qs.QueryStatementHash
    join qt in qtFilter on qs.QueryTableHash equals qt.QueryTableHash
    where t.CallTypeId == 64
    orderby t.Sequence
    select new
    {
        Name = qt.TableNames
    };

The problem is that in the final query, it generates an AND clause for the 2 LIKE filters:

WHERE ([Filter1].[Statement] LIKE @searchTerm) AND 
    ([Extent3].[TableNames] LIKE @searchTables)

How do I get that to be an OR clause?

Note: if you are wondering why I am using ESQL here, it is because of this: http://stackoverflow.com/questions/3095781/how-to-use-sql-like-with-linq-to-entities

A: 

Although my LinqToEntities experience is limited, my reading of the reference materials suggests this might work:

var joins =
  from t in entities.TraceLines 
  join qs in entities.QueryStatements
    on t.QueryStatementHash equals qs.QueryStatementHash 
  join qt in entities.QueryTables
    on qs.QueryTableHash equals qt.QueryTableHash
  select new {t, qs, qt};

var filtered = joins
  .Where("it.Statement LIKE @searchTerm OR it.TableNames LIKE @searchTables"
    new ObjectParameter("searchTerm", searchTerm)
    new ObjectParameter("searchTables", searchTerm))
  .Where(x => x.t.CallTypeId == 64)

var orderedAndProjected =
  from x in filtered
  order by x.t.Sequence
  select new 
  { 
    Name = qt.TableNames 
  };

I'm baffled at what the "it" is for in those opaque strings. What a strange choice of codeword.


Of course, if you switch to LinqToSql, you can simply write this (but you already knew that):

var query = 
  from t in myDC.TraceLines
  from qs in t.QueryStatements
  from qt in qs.QueryTables
  where t.CallTypeId == 64
  where SqlMethods.Like(qs.Statement, searchTerm)
     || SqlMethods.Like(qt.SearchTables, searchTerm)
  order by t.Sequence
  select new
  {
    Name = qt.TableNames
  };
David B
I am not sure what the 'it' is for either. I know it doesn't work without it. If you look in the debugger, it seems to be the name applied to the table currently being reference in the query.
esac