I want to pass the anonymous type variable "linq" to the method LinqToDataTable to get Datatable loaded with values of linq..If i do like
case "Profession":
{
var linq=......;
DataTable dt=new LinqToDataTable(linq);
return dt.
}
it works fine..but outside of the scope i couldnt able to use linq as i dont know how to initialize var as empty...if i use
Iqueryable<> linq=null; i have to specify type inside <>..In my scenario wat i have to specify as type?????DataClassContext??
public DataTable GetSelectionListData(string type, string selected, string filter, string filterType)
{
DataClassContext db=new DataClassContext();
//IQueryable linq=null; // This is where the problem arise.How initialize query type linq to null
switch (type)
{
case "Alert Id":
{
if (filter.Length > 0)
{
if (filterType.IndexOf(";") > -1)
{
linq=--;
}
else
{
linq=---;
}
}
}
else
{
linq=---;
}
break;
}
case "Order Status":
{
linq=--;
break;
}
case "Profession":
{
linq=--;
break;
} }
}
DataTable dt = LINQToDataTable(linq);
return dt;
}
public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
==typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ?DBNull.Value :pi.GetValue
(rec,null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}