Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Linq 无法创建类型为“YYY”的常量值。在此上下文中仅支持基元类型或枚举类型_Linq_Entity Framework_Linq To Entities - Fatal编程技术网

Linq 无法创建类型为“YYY”的常量值。在此上下文中仅支持基元类型或枚举类型

Linq 无法创建类型为“YYY”的常量值。在此上下文中仅支持基元类型或枚举类型,linq,entity-framework,linq-to-entities,Linq,Entity Framework,Linq To Entities,我有一个实体Person和一个包含Person集合的视图模型 public class ViewModel { public string Name { get; set; } public string Description { get; set; } public int Project { get; set; } public ICollection<PersonsViewModel> PersonCollection { get; set; } }

我有一个实体Person和一个包含Person集合的视图模型

public class ViewModel
{
   public string Name { get; set; }
   public string Description { get; set; }
   public int Project { get; set; }
   public ICollection<PersonsViewModel> PersonCollection { get; set; }
}

public class PersonsViewModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
以下是我的尝试:

1e:

2e:

根据博客文章,3e:

ICollection<Person> prsn = (from st in new PersonRepository().GetAll()
                            from qw in cm.PersonCollection
                            where st.Id.Equals(qw.Id)
                            select st).ToList();
我试图做的是,根据视图模型中的人员id,从datacontext中选择人员实体。在所有3次尝试中,我都做了更多尝试,但我丢失了计数,结果出现了标题中所述的运行时错误

我在这里也发现了同样的问题,但是很难将它与我的进行比较,因为没有像model/entity这样的额外代码

有人能给我指出正确的方向吗?

你可以这样做

var Ids=vm.PersonCollection.Select(y => y.Id).ToArray();

ICollection<Person> prsn =
 new PersonRepository().GetAll().Where(x => Ids.Contains(x.Id)).ToList();
ICollection<Person> prsn = (from st in new PersonRepository().GetAll()
                            from qw in cm.PersonCollection
                            where st.Id.Equals(qw.Id)
                            select st).ToList();
var Ids=vm.PersonCollection.Select(y => y.Id).ToArray();

ICollection<Person> prsn =
 new PersonRepository().GetAll().Where(x => Ids.Contains(x.Id)).ToList();