C# 无法比较类型为';System.Collections.Generic.ICollection`1';。仅支持基元类型、枚举类型和实体类型
执行以下代码时出错,LINQ新手,无法找到如何分配子对象“StudentAddresses”值,该值将作为icollection键入到students模型中C# 无法比较类型为';System.Collections.Generic.ICollection`1';。仅支持基元类型、枚举类型和实体类型,c#,linq,icollection,C#,Linq,Icollection,执行以下代码时出错,LINQ新手,无法找到如何分配子对象“StudentAddresses”值,该值将作为icollection键入到students模型中 IList<StudentViewmodel> students = null; using (var CTX = new StudentEntities()) { students = CTX.Students.Include("JP").Select( s => new StudentView
IList<StudentViewmodel> students = null;
using (var CTX = new StudentEntities())
{
students = CTX.Students.Include("JP").Select(
s => new StudentViewmodel()
{
Id = s.StudentId,
FirstName = s.FirstName,
LastName = s.LastName,
address = new AddressViewmodel()
{
// Address1 = s.StudentAddresses.Select(t => t.Address1).ToString()
Address1 = s.StudentAddresses.OfType<AddressViewmodel>().FirstOrDefault().Address1.ToString()
}
}
).ToList<StudentViewmodel>();
}
IList students=null;
使用(var CTX=new studententies())
{
students=CTX.students.Include(“JP”)。选择(
s=>new StudentViewmodel()
{
Id=s.StudentId,
FirstName=s.FirstName,
LastName=s.LastName,
地址=新地址视图模型()
{
//Address1=s.StudentAddresses.Select(t=>t.Address1.ToString())
Address1=s.StudentAddresses.OfType().FirstOrDefault().Address1.ToString()
}
}
).ToList();
}
我想这对你有帮助
IList<StudentViewmodel> students = null;
using (var CTX = new StudentEntities())
{
students = CTX.Students.Include("JP").ToList().Select(
s => new StudentViewmodel()
{
Id = s.StudentId,
FirstName = s.FirstName,
LastName = s.LastName,
address = new AddressViewmodel()
{
// Address1 = s.StudentAddresses.ToList().Select(t => t.Address1).ToString()
Address1 = s.StudentAddresses.OfType<AddressViewmodel>().FirstOrDefault().Address1.ToString()
}
}
).ToList<StudentViewmodel>();
}
IList students=null;
使用(var CTX=new studententies())
{
students=CTX.students.Include(“JP”).ToList().选择(
s=>new StudentViewmodel()
{
Id=s.StudentId,
FirstName=s.FirstName,
LastName=s.LastName,
地址=新地址视图模型()
{
//Address1=s.StudentAddresses.ToList().Select(t=>t.Address1.ToString())
Address1=s.StudentAddresses.OfType().FirstOrDefault().Address1.ToString()
}
}
).ToList();
}
如果comment access属性出现问题,请再次测试。并确保您在哪里遇到问题。谢谢我的问题已解决,下面是更新的代码
IList<StudentViewmodel> students = null;using (var CTX = new StudentEntities())
{
students = CTX.Students.Include("JP").Select(
s => new StudentViewmodel()
{
Id = s.StudentId,
FirstName = s.FirstName,
LastName = s.LastName,
address = new AddressViewmodel()
{
Address1 = s.StudentAddresses.FirstOrDefault().Address1 ,
Address2 = s.StudentAddresses.FirstOrDefault().Address2,
City = s.StudentAddresses.FirstOrDefault().City,
State = s.StudentAddresses.FirstOrDefault().State
}
}
).ToList<StudentViewmodel>();
}
IList students=null;使用(var CTX=new studententies())
{
students=CTX.students.Include(“JP”)。选择(
s=>new StudentViewmodel()
{
Id=s.StudentId,
FirstName=s.FirstName,
LastName=s.LastName,
地址=新地址视图模型()
{
Address1=s.StudentAddresss.FirstOrDefault().Address1,
Address2=s.StudentAddresss.FirstOrDefault().Address2,
City=s.StudentAddresss.FirstOrDefault().City,
State=s.studentAddresss.FirstOrDefault().State
}
}
).ToList();
}
comment address属性并再次运行。如果有问题,请再次发送消息。我更改答案。