Función que devuelve una lista genérica

I have WPF application project and I'm using EF. I want something like this.

NorthwindEntities nwModel = new NorthwindEntities();
Employees emp;
List mylist = GenericFunc("emp.FirstName"); => returns a List<string>
and
List mylist = GenericFunc("emp.PostalCode"); => returns a List<int>

public static List<T> GenericFunc<T>(string myfield)
{...}

Sorry if I made any mistakes.

preguntado el 09 de marzo de 12 a las 14:03

What code do you have so far? What's the question exactly? -

2 Respuestas

You'd be better off using LINQ instead of your "GenericFunc" Like so:

List<string> nameList = (from e in emp select emp.FirstName).ToList();
//and
List<int> postCodeList = (from e in emp select emp.PostalCode).ToList();

respondido 09 mar '12, 14:03

If you use a string paramter to represent a filed so :

public static List<T> GenericFunc<T>(string myfield, string TABLENAME)
{
   return nwModel.ExecuteQuery<T>("SELECT " + myfield + " FROM " + TABLENAME).ToList();
}

respondido 09 mar '12, 14:03

You got me:p, I want to make a dynamic query(select) ; I've added System.Data.Linq and I still can't use after nwModel.ExecuteQuery<T> ?! - Misión

Here is my original post in WPF forum : social.msdn.microsoft.com/Forums/en-US/wpf/thread/… - Misión

My code works LINQ(Dbml context), for EF try : msdn.microsoft.com/en-us/library/bb738694.aspx - Amén Ayach

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.