Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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);
clientContext.ExecuteQuery();

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 = @"
    <View>
        <ViewFields>
            <FieldRef Name='ID'/>
            <FieldRef Name='Title'/>
        </ViewFields>
        <Query>
            <Where>
                <Contains>
                    <FieldRef Name='Title' />
                    <Value Type='Text'>van</Value>
                </Contains>
            </Where>
        </Query>
        <RowLimit>10</RowLimit>
    </View>";

clientContext.Load(listItems);
clientContext.ExecuteQuery();

It works fine.

What is the explanation for this?

share|improve this question

2 Answers 2

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."

share|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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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