无法从'System.Collections.Generic.List<>'转换至 ... [英] cannot convert from 'System.Collections.Generic.List<>' to ...

查看:386
本文介绍了无法从'System.Collections.Generic.List<>'转换至 ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我的代码有错误

我在谷歌搜索但我无法解决它。

我的代码在mvc asp:

  if (ModelState.IsValid)
{
var y = new Student();

if (t == null
{
// -------
var 日期= Shamsi2Miladi(student.BirthDate.ToString());

IEnumerable< Student> ww = _repository.Students;
列表< Student> newStudent =
ww.Select(
a = > new 学生()< span class =code-comment> // 错误行
{
Name = a.Name,
密码= a.Password,
教育= a.Education,
性别= a.Gender,
CityId = a.CityId,
关于= a.About,
BirthDate = Date,
EmailAddress = a.EmailAddress,
IsActive = a.IsActive,
StudentID = a.StudentID,
RecordDate = a.RecordDate,
})
.Tolist();

y = _repository.Students.Add(newStudent); // 错误行

_repository.SaveChanges();
}

return Json( new {Result = OK,Record = y});
}



// ---------------------------- -----------

我的错误:

1-'System.Data.Entity.DbSet< myprojectjtable.models的最佳重载方法匹配.student> .Add(MyProjectJTable.Models.Student)'有一些无效的参数



2-无法从'System.Collections.Generic.List< myprojectjtable.models转换.student>'到'MyProjectJTable.Models.Student'



3-错误4'MyProjectJTable.Models.Student'不包含带0参数的构造函数



谢谢你的答案。

解决方案

错误信息非常明确:

'MyProjectJTable.Models.Student'不包含带0参数的构造函数



这意味着你不能这样做:

 a = >   new 学生()

你好编辑以查看您的Student类并找出可用的构造函数,并使用适当的参数调用其中一个。

我们不能为您执行此操作:我们看不到您的屏幕,访问你的硬盘,或读你的想法!



其他错误是类似的问题:列表< Student>没有转换。到你的模型类 - 所以看看你的模型代码中有什么,因为我们不能!


1: newStudent 是一个列表< Student> 但是 System.Data.Entity.DbSet.Add(..)(你是使用 repository.Students.Add(newStudent)进行调用只需要一个学生,而不是一个List。要添加学生列表,请拨打AddRange而不是添加。



修复后,下一个问题是 y 类型为学生但AddRange将返回 IEnumerable< Student> 。你的初始化 var y = new Student(); 在顶部是多余的(因为你从不使用那个单独的Student实例),除了因为错误而无法正常工作.3。删除该行在顶部并写入:

 IEnumerable< Student> y = _repository.Students.Add 范围(newStudent); 





评论后编辑:当您使用EF4时,没有AddRange(..)方法。解决方法是:

  foreach (学生 in  newStudent)
{
_repository.Students.Add(student);
}





2:当你修正错误1时,错误2应该消失。



3:学生没有无参数构造函数。所以你不能像这样创建一个Student实例:

  var  xyz =  new 学生(); 

// 也不是这样的:
var xyz = new Student(){Name = name,Password = pwd, / * etc * / };

查看Student类的定义并查看其中的签名公共构造函数。可能有像Student(字符串名称)这样的构造函数,或者您在ww.Select中初始化的所有字段都是必需的。所以你必须这样写:

  var  xyz = 学生(姓名,代码, / *   etc * / ); 


hello friends
I have an error on my code
I searched in google but I could not solve it.
my code in mvc asp:

if (ModelState.IsValid)
{
   var y = new Student();

   if (t == null)
   {
      // -------
      var Date = Shamsi2Miladi(student.BirthDate.ToString());

      IEnumerable<Student> ww = _repository.Students;
      List<Student> newStudent =
         ww.Select(
            a => new Student() // error line
            {
               Name = a.Name,
               Password = a.Password,
               Education = a.Education,
               Gender = a.Gender,
               CityId = a.CityId,
               About = a.About,
               BirthDate = Date,
               EmailAddress = a.EmailAddress,
               IsActive = a.IsActive,
               StudentID = a.StudentID,
               RecordDate = a.RecordDate,
            })
      .Tolist();

      y=_repository.Students.Add(newStudent); // error line

      _repository.SaveChanges();
   }

   return Json(new { Result = "OK", Record = y });
}


//---------------------------------------
my errors:
1- The best overloaded method match for 'System.Data.Entity.DbSet<myprojectjtable.models.student>.Add(MyProjectJTable.Models.Student)' has some invalid arguments

2- cannot convert from 'System.Collections.Generic.List<myprojectjtable.models.student>' to 'MyProjectJTable.Models.Student'

3- Error 4 'MyProjectJTable.Models.Student' does not contain a constructor that takes 0 parameters

thank you from your answers.

解决方案

The error message are pretty explicit:

'MyProjectJTable.Models.Student' does not contain a constructor that takes 0 parameters


Which means you can't do this:

a => new Student()

You need to look at your Student class and find out which constructors are available, and call one of those with the appropriate parameters.
We can't do that for you: we can't see your screen, access your HDD, or read your mind!

The other errors are similar problems: there is no conversion from a List<Student> to your model class - so look and see what there is in your model code because we can't!


1: newStudent is a List<Student> but System.Data.Entity.DbSet.Add(..) (which you're calling with repository.Students.Add(newStudent)) takes a single Student, not a List. To add a List of Students, call AddRange instead of Add.

When you fix that, the next problem will be that y is of Type Student but AddRange will return an IEnumerable<Student>. Also your initialization var y = new Student(); at the top is superfluous (because you never use that single Student instance), aside from not working because of error 3. Remove that line at the top and write instead:

IEnumerable<Student> y = _repository.Students.AddRange(newStudent);



edit after comments: As you're working with EF4, there's no AddRange(..) method. The workaround would be:

foreach(Student student in newStudent)
{
    _repository.Students.Add(student);
}



2: Error 2 should vanish when you fix error 1.

3: Student doesn't have a parameterless constructor. So you can't create an instance of Student like this:

var xyz = new Student();

//and also not like this:
var xyz = new Student() { Name = name, Password = pwd, /*etc*/ };

Look at the definition of the Student class and look there for the signature of the public constructor(s). There might be a constructor like Student(string name) or maybe all of the fields that you initialize in the ww.Select.. are required. So you have to write them like this:

var xyz = new Student(name, pwd, /*etc*/);


这篇关于无法从'System.Collections.Generic.List&lt;&gt;'转换至 ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆