Selecting only one column linq lambda query asp.net

I am quite new to entity framework and linq but basically I am using data first and my database has a table called tblNumbers and it has 2 columns, an id column and a numbers column which is populated with int values, I want to populate only the number values into my list but when I try do this I get an error saying that I cannot implicitly convert system.collections.generic.list< int> to system.collections.generic.list<projectname.Models.tblNumber>. I am not sure where to go from this, any help would be much appreciated.

Here is my code:

private DatabaseEntities db = new DatabaseEntities();

public ActionResult MyAction()
{
    db.Configuration.ProxyCreationEnabled = false;
    List<tblNumber> numbers = db.tblNumbers.Select(column => column.numbers).ToList();
    return View();
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your List<tblNumber> numbers is expecting a list of tblNumber type and you are selecting column.numbers only

var numbers = db.tblNumbers.Select(column => column.numbers).ToList();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x