0
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?

2
  • 1
    how about using .ToString() Commented Mar 14, 2012 at 6:52
  • 2
    I think we're missing something here. I don't see ContactId referenced anywhere, and unless ListItem not the ListItem I think it is, it should be new ListItem { Value = s.studentId.ToString(), Text = s.studentName } Commented Mar 14, 2012 at 6:52

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

Did you try 'ToString()'?

var studentId = s.studentId.ToString();
var query = from s in student select new ListItem { ID = studentId, Name = s.studentName };

1 Comment

var query = from s in student select new ListItem { ID = s.studentId.ToString(), //Throws exception: ToString is not supported in linq to entities. Name = s.Name }; I have tried this also..
0

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());

Comments

0

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
                        };

Comments

0

create a new variable with var type and assign the value of studentid to that vaiable and use the new one in your linq

2 Comments

I have tried the following var query = from s in student select new ListItem { ID = s.studentId.ToString(), //Throws exception: ToString is not supported in linq to entities. Name = s.Name };
then declare your ID as string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.