0

Just having some problems with LINQ and Microsoft CRM and I hope you can help.

in SQL terms I am thinking along the lines of:

select * from table where id in ("1", "2", "3")

I have this code and it works fine when retrieving records off a SQL Server.

 var _records = from _adUserDatas in _adUserDataDBDataContex.ADUserDatas
     where
          _list.Contains(_adUserDatas.id)
     orderby _adUserDatas.fan
     select _adUserDatas;

When I changed it to use the contacts entity in Microsoft CRM, it is throwing an exception and it says my where clause is not valid.

Any ideas how to get it to work?

thanks

ps. I found this stackoverflow discussion but I'm getting errors on the myset.where part.

Community
  • 1
  • 1
mrjayviper
  • 2,258
  • 11
  • 46
  • 82

1 Answers1

1

CRM LINQ Context does not support this. Please check the LINQ limitations here.

LINQ Operator: where

Limitations: The left side of the clause must be an attribute name and the right side of the clause must be a value. You cannot set the left side to a constant. Both the sides of the clause cannot be constants.

There is a workaround called Dynamic Linq. Have a look if it can help you.

With Dynamic Linq, something like following shoule work for you.

var adUserDatas = _adUserDataDBDataContex.ADUserDatas
string[] _list = new[] { "1", "2", "3" };
string whereClause = String.Empty;

foreach (var id in _list)
{
    whereClause += string.Format("_adUserDatas.id = \"{0}\" OR ", id);
}

adUserDatas = adUserDatas.Where(whereClause.Substring(0, whereClause.Length - 4));

var query = from n in adUserDatas
        select n;

Ref: Link

Community
  • 1
  • 1
Scorpion
  • 4,495
  • 7
  • 39
  • 60