Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将foreach转换为Linq会导致Microsoft.Xrm.Sdk.dll中出现InvalidOperationException_C#_Linq_Linq To Entities_Dynamics Crm 2013 - Fatal编程技术网

C# 将foreach转换为Linq会导致Microsoft.Xrm.Sdk.dll中出现InvalidOperationException

C# 将foreach转换为Linq会导致Microsoft.Xrm.Sdk.dll中出现InvalidOperationException,c#,linq,linq-to-entities,dynamics-crm-2013,C#,Linq,Linq To Entities,Dynamics Crm 2013,我有一个Linq查询正在连接到CRM 2013 Organization.svc web服务。查询工作正常,因为当我枚举结果中的项时,我得到了预期值 当我试图将MVC5站点的项目列表转换为SelectListItems列表时,出现了问题 如果我有这样的代码: List<SelectListItem> driverList = new List<SelectListItem>(); foreach (var driver in drivers) { driverLi

我有一个Linq查询正在连接到CRM 2013 Organization.svc web服务。查询工作正常,因为当我枚举结果中的项时,我得到了预期值

当我试图将MVC5站点的项目列表转换为SelectListItems列表时,出现了问题

如果我有这样的代码:

List<SelectListItem> driverList = new List<SelectListItem>();

foreach (var driver in drivers)
{
   driverList.Add(new SelectListItem() {Text = driver.account_name, Value = driver.account_id.ToString()});
}
List<SelectListItem> driverList = drivers.Select(driver => new SelectListItem() {Text = driver.account_name, Value = driver.account_id.ToString()}).ToList();
List driverList=new List();
foreach(驱动程序中的var驱动程序)
{
添加(新的SelectListItem(){Text=driver.account\u name,Value=driver.account\u id.ToString()});
}
我没有被抛出错误

但是,如果我尝试将其转换为Linq语句(使用Resharper),如下所示:

List<SelectListItem> driverList = new List<SelectListItem>();

foreach (var driver in drivers)
{
   driverList.Add(new SelectListItem() {Text = driver.account_name, Value = driver.account_id.ToString()});
}
List<SelectListItem> driverList = drivers.Select(driver => new SelectListItem() {Text = driver.account_name, Value = driver.account_id.ToString()}).ToList();
List driverList=drivers.Select(driver=>new SelectListItem(){Text=driver.account\u name,Value=driver.account\u id.ToString()).ToList();
我从运行时返回如下错误:左'String'和右'Nullable'1'参数无效


在这种情况下,我必须坚持使用foreach吗?

什么是
驱动程序
?您没有在代码中显示定义-我的第一个猜测是,它不是
列表
,如果您将其更改为
drivers.ToList()。选择
,它将起作用。drivers是针对连接到CRM的OrganizationContext执行的linq查询。它返回一个匿名对象,该对象由account\u name和account\u id对象组成。account\u name是一个字符串,account\u id是一个Guid。它应该可以工作。您可以发布
驱动程序
实体类吗?