使用IEnumerable <t> </t>的参数调用Generic Method

时间:2014-03-17 12:31:52

标签: c# asp.net linq generics

这是要调用的方法:

public static DataTable LinqToDataTable<T>(IEnumerable<T> varlist)
{
   ..
}

这是查询:

var list = from c in db.Person
           join i in db.Employment on c.ID equals i.personID
           join z in db.aspnet_UsersInCompany on i.ID equals z.EmploymentID
           join k in db.aspnet_Users on z.userID equals k.UserId
           join iy in db.WorkPlace on i.WorkplaceID equals iy.ID
           where i.Date == null && (db.Profile.Any(p => p.personID == c.ID) &&
           db.Employment.Any(y => y.personID == c.ID) && c.companyID == companyID2 && iy.ID == WorkPlaceID2)
           select new
           {
                no     = c.registryNo,
                name   = c.name + " " + c.surname,
                istart = i.EmpStartDate,
                isy    = iy.workplaceName,
                email  = k.UserName,
                IDs    = i.ID,
                IDM    = c.ID,
           };

我想调用LinqToDataTable方法,将list作为参数发送。 我尝试使用<T>,但它不适用。

DataTable datatable = Tools.LinqToDataTable<>(list.ToList());

1 个答案:

答案 0 :(得分:3)

完全删除通用参数:

DataTable datatable = Tools.LinqToDataTable(list.ToList());

编译器应该从那里推断出泛型类型。您甚至可以直接通过列表(根据定义,查询可以枚举)。