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

13,170

Solution 1

device.ID = (from a in db.FilterTree select a.ID).First();

Linq query is lazy, and executes only after you request the value

BTW don't forgot to close the context, otherwise you will leak connections

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

Solution 2

As far as the frameworks knows that query may have more than one object so use it like this:

device.ID = (from a in db.FilterTree select a.ID).FirstOrDefault();
Share:
13,170
Cristian
Author by

Cristian

Updated on August 12, 2022

Comments

  • Cristian
    Cristian almost 2 years

    I have a problem sending data from the database to a list in the Controller. I am new to C# and MVC so any help will be useful! 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;
        }
    

    Thanks & Happy Holidays!! :)

  • Mike Perrenoud
    Mike Perrenoud over 10 years
    @Mihai-CristianRotariu, with that being the case, make sure to accept it as the answer when the time has elapsed.