Given a string like this:
var filterOptions = "First Bank";
And a table that has a Grantee column and a Grantor column, how do I create a query with EF that will generate the following where clause:
... where (Grantor like '%First%' and Grantor like '%Bank%') or (Grantee like '%First%' and Grantee like '%Bank%')
Keep in mind that filterOptions is a parm that is passed in, so it could just as easily contain 3 or 4 words rather than just two … in which case each column would need additional like clauses.
If it weren’t for the OR part of this, I would do the following (which will help give a better idea of what I’m trying to accomplish.
foreach(var word in filterOptions)
{
var text = "%" + word + "%";
query = query.Where(r => SqlMethods.Like(r.Grantee,text));
}
Like I said, its trying to add the OR portion with the other column that throws me for a loop.
First, Read the blog below, then you will know how to do the OR operation:
http://www.albahari.com/nutshell/predicatebuilder.aspx
Second, If you’re using Entity Framework, read another blog, it has a better “predicatebuilder” implematation for Entity Framework:
http://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/