When using 'Contains' in Linq query:

var camlQuery = new CamlQuery();
var listItems = list.GetItems(camlQuery);

var itemQuery = listItems.Where(i => i.FieldValuesAsText["Title"].Contains("van")).Include(
    item => item["ID"],
    item => item["Title"]);

var itemsFound = clientContext.LoadQuery(itemQuery);

I get an error like: "The 'Contains' member cannot be used in the expression."

But when using a CamlQuery like:

var camlQuery = new CamlQuery();
camlQuery.ViewXml = @"
            <FieldRef Name='ID'/>
            <FieldRef Name='Title'/>
                    <FieldRef Name='Title' />
                    <Value Type='Text'>van</Value>


It works fine.

What is the explanation for this?

  • I am unable to comment on the question so just posting my question under the answers. Did you find a solution to this issue? How were you able to achieve what is intended through the where clause? You inputs will be of great help! Thanks – jkr Oct 2 '15 at 9:30

Unfortunately this is a limitation of LINQ. I've had exactly the same error when using LINQ to SQL.

The problem is that LINQ only knows how to map certain operations. We can see string.Contains() and the <Contains>element are analogous to each other, but LINQ just sees an object's method and says "Does this match any of my recognised operations? Nope. Fail."

| improve this answer | |

I am not sure, but FieldValuesAsText will return FieldStringValues.

So you need to convert it to string first and then need to check with contain option.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.