views:

202

answers:

3

Customer.text is a field in an T-SQL DB (that I do not control and thus may not alter) of type "text".

I'd like to do something like this:

List<string> compare = new List<string>();
compare.Add("one");
compare.Add("two");

var q = from t in customer
        where t.text.Contains( compare.First())
        select t;

this will work.

But now I'd like to do something like: (!NOT WORKING!)

var q = from t in customer
        where compare.Contains( t.text )
        select t;

How can I achieve this? Is it even possible?

EDIT: The problem is obviously not exactly clear: A text column in SQL cannot be queried using "=" but only with LIKE. Thus the compare.Contains( t.text ) will result in an error, as it is converted into a query using "=".

What I did not tell - I thought it is irrelevant - is, that I use LINQ-to-ORM (LLBLGen in this case). What I tried instead:

var q = from t in customer
        where compare.Any( x => t.text.Contains(x) )
        select t;

Now this did not work also. Currently I'm not at work, but the exception was something with a ConstantExpression not being convertable into a SetExpression.

I hope this gave some clarification.

EDIT2:

Joseph pointed this out to me: PredicateBuilder. It creates an Expression on a given ObjectType. Now my problem is, that my type is an anonymous type out of multiple joins. Is there an easy or elegant way to handle this?

+1  A: 

Now I might be missing something, but your code looks like it should work. Did you include the namespaces at the top of the file?

using System.Linq;
using System.Linq.Expressions;

You could also rewrite it without the Linq2Sql syntax, like:

var q = customer.Where(c => compare.Contains(c.text));
Andomar
Nitpick: Linq2Sql is not syntax, it's the library. The syntax sugar you're alluding to is called "query expressions". Your code is still Linq2Sql, but written in method syntax.
Martinho Fernandes
A: 

You have to convert the text field to string

        var query = from t in dataContext.table
                    where compare.Contains(t.textField.ToString())
                    select t;
Joseph Connolly
I tried that one shortly. It did not give any errors, but it was as if the where clause was not there (meaning: the results contained also textFields with values not in the List)
StampedeXV
After reading the comments, you might have to create a Linq expression to create your complex where clause.
Joseph Connolly
+1  A: 

You could build your query using LinqKit's free predicate builder class. Here is a blog post which describes its use and has a link to the download site.

http://thecodeslinger.wordpress.com/2008/10/28/linqkit-predicatebuildert-goodness/

Below is a code sample from the post

    //First get a list of keywords that match the description entered.
                string[] parts = txtInclude.Text.Split(new[] {‘ ‘});
                string[] noparts = null;
                if(txtButNot.Text.Trim().Length > 0)
                    noparts = txtExclude.Text.Trim().Split(new[] {‘ ‘});

                var pred = PredicateBuilder.True<Pet>();
   //here is where you would loop through your compare object
                parts.ForEach(p => pred = pred.And(pl => pl.description.Contains(p)));
                if(noparts != null)
                    noparts.ForEach(p => pred = pred.And(pl => !pl.description.Contains(p)));

                var pets = from s in db.Pets.Where(pred)
                        select s;
Joseph Connolly
In general this would work. But I'm Joining a lot there and I can't provide a real Type (it's an anonymous Type I have after the join), so the predicate is not working.
StampedeXV