var query = from s in student
select new ListItem
{
ID = s.studentId,
Name = s.studentName
};
When I tried to execute the code I got the error
/ /Cannot implicitly convert type 'int' (studentId) to 'string' (ID).
Is there anyway I can achieve this?
Did you try 'ToString()'?
var studentId = s.studentId.ToString();
var query = from s in student select new ListItem { ID = studentId, Name = s.studentName };
Since you are using LINQ to Entities ToString won't work for you instead you will have to try out
SqlFunctions.StringConvert((double)s.studentId)
//since there is no overload for int
Check this out Sql Functions
使用var类型创建一个新变量,并将studentid的值赋给该变量,并在linq中使用新变量
The ListItem does not contain an ID property. Try using one of its constructors;
var query = from s in student select new ListItem(s.studentName, s.studentId.ToString());
Use Convert.ToString() method.....Convert.ToString(obj) doesn't need to presume the object is not null (as it is a static method on the Convert class), but instead will return String.Empty if it is null.....
var query = from s in student
select new ListItem
{
ID = Convert.ToString(s.studentId),
Name = s.studentName
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.