Jude
Jude

Reputation: 2433

Invoking Generic Method with the parameter of IEnumerable<T>

This is the method to be invoked:

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

This is the query:

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,
           };

I would like to invoke LinqToDataTable method, sending the list as an argument. I attempted to use <T> but it's not applicable.

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

Upvotes: 1

Views: 94

Answers (1)

ioctlLR
ioctlLR

Reputation: 1232

Drop the generic parameter entirely:

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

The compiler should infer the generic type from there. You might even get away with passing list directly (by definition, queries are enumerable).

Upvotes: 3

Related Questions