Cannot convert type 'System.Linq.IQueryable <int>' to 'int'

I have a problem sending data from a database to a list in the controller. I am new to C # and MVC, so any help would be helpful! The code follows

public static List<FilterTree> GetAllUsers() { FilterTreeDBContext db = new FilterTreeDBContext(); var userList = new List<FilterTree>(); var device = new FilterTree(); //This is the line where I get the error device.ID = from a in db.FilterTree select a.ID; userList.Add(device); return userList; } 

Thank you and happy holidays !! :)

+6
source share
2 answers
 device.ID = (from a in db.FilterTree select a.ID).First(); 

The Linq query is lazy and only runs after a value request

BTW does not forget to close the context, otherwise you will leak connections

 using (var db = new FilterTreeDBContext()) { ... return userList; } 
+12
source

Since structures know that a query can have more than one object, use it like this:

 device.ID = (from a in db.FilterTree select a.ID).FirstOrDefault(); 
+2
source

All Articles