I am trying to return a generic list from a method and am having a bit of trouble with the syntax. Here's what I have:
class GenList<T>
{
private List<T> list;
private SampleEntities context = new SampleEntities();
public GenList(List<T> contactList)
{
list = contactList;
populateList();
}
public List<T> populateList()
{
var contacts = (from c in context.Contacts select new T{ c.FirstName, c.LastName };
list = contacts.ToList();
return list;
}
}
I am creating an instance from the main method:
class Program
{
static void Main(string[] args)
{
List<Contact> contactList = null;
GenList<Contact> list = new GenList<Contact>(contactList);
foreach (Contact c in list)
Console.Write(c.FirstName + "\n");
Console.ReadLine();
}
}
I am getting an error on line:
var contacts = (from c in context.Contacts select new T{ c.FirstName, c.LastName };
reading: cannot initialize T because it does not implement Collections.Generic.IEnumerable.
If anyone has any ideas on how to get rid of this error it would be greatly appreciated!
Thanks in advance!

New Topic/Question
Reply




MultiQuote



|