I have a problem with ToList() method and basically I'm trying to make a function that will return a var linq query convert to a list and here the the function.
List<UsersTabPage> GetFirstOne()
{
using (MCMDataContext db = new MCMDataContext())
{
MCM.User user = new MCM.User();
var firstone = (from oneUser in db.Users
where oneUser.ID == user.ID
select oneUser).Single();
return firstone.ToList();
}
}
Here are the librarys that I'm currently using in the application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using MahApps.Metro.Controls.Dialogs;
using System.IO;
using System.Data;
If there is any library needed, Please comment below or post an answer. Here is the error.
'MCM.User' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'MCM.User' could be found (are you missing a using directive or an assembly reference?) If you know any answer please post it below. Thank you.
You're calling Single()
, which means you've got one result. ToList()
is an extension method on IEnumerable<T>
.
If you want to create a list with just that one element, you could write:
return new List<UsersTabPage> { firstone };
... but it seems more likely that either you should get rid of the Single()
call or you should just make your method return UsersTabPage
.
On the other hand, it looks like the type of firstone
is a User
, not a UsersTabPage
- we don't know anything about the relationship between those two types, so you probably want to revisit that aspect, too.
See more on this question at Stackoverflow