Pier Giorgio Misley February 2016

c# linq crm select where contains

I'm trying to select from CRM entity with "contains" key.

I tryed this:

var results = crm.new_supplycontractSet
    .Where(x => x.new_city != null && 
        x.new_city.Name.Contains("myChars")) 
    .ToList();

but it give me this error:

Invalid 'where' condition. An entity member is invoking an invalid property or method.

and this:

var result = (
    from c in crm.new_supplycontractSet
    from a in crm.new_comuneSet 
    where a.new_name.Contains(comune)
    where c.new_city.Id == a.Id
    select c)
    .ToList();

But i can't figure out how to do it. The second try gives me this error:

A 'SelectMany' operation must be preceeded by a 'Where' operation that filters by an entity ID.

How can I select by a contains filter? "x.new_city" is an entity ref from crm.new_comuneSet.

PS: I've just read something about the inaccessibility of the "entity.entityRef.Name.Contains()" because the "Name" property is not ground level and so it's not available for the ".contains" check.

Answers


Arturo Perea February 2016

you can try converting your field and your filter to lower or upper case.

  var results = crm.new_supplycontractSet
    .Where(x => x.new_city != null && 
        x.new_city.Name.ToLower().Contains(("myChars").ToLower())) 
    .ToList();

I think is better if you use StartsWith instead (and if is possible).


Pier Giorgio Misley March 2016

At the end i got it. instead of contains clause, I had to install SqlClient and use the following:

  results = (from x in crm.new_supplycontractSet
                           where x.new_city != null
                           where x.new_address != null
                           where SqlMethods.Like(x.new_city.Name, "city")
                           where SqlMethods.Like(x.new_address.Name, "street")
                           select ....).ToList();

hope this will help someone else :)

Post Status

Asked in February 2016
Viewed 1,166 times
Voted 11
Answered 2 times

Search




Leave an answer