In the above code, I cannot put IList Datasource as its parameter where SelEmployeeByDesignationName_Result is of type ComplexObject of EntityFramework (EF). I need this function to take a parameter which would be common to all and convert such ComplexObject
to an IList so that I can pass any ComplexObject from the page which will call this common FillGridView function.
I can provide with my code snippet if you want clear explanation. Please suggest.
Satish Chilkury
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
Then you can change you function to accept IQueryable<T>, where T is your base class for entities
public static void FillGridView(GridView gv, IQueryable<T> DataSource, string sortExpression, string sortDirection) where T: NameOfBaseClassForYourEntities
As I understand from your code you need to parse IQueryable<T> to DataTable inside of FillGridView function. Here is another link, which descripbes how you can convert IEnumerable<T> to DataTable, which in your case might be much of help, because all you
have to do, is to call ToList() on your DataSource and use the helper function
To add to my earlier conversation ... this is the code for converting to ToDataset
public static DataSet ToDataSet<T>(this IList<T> list)
{
Type elementType = typeof(T);
DataSet ds = new DataSet();
DataTable t = new DataTable();
ds.Tables.Add(t);
//add a column to table for each public property on T
foreach (var propInfo in elementType.GetProperties())
{
t.Columns.Add(propInfo.Name, propInfo.PropertyType);
}
//go through each property on T and add each value to the table
foreach (T item in list)
{
DataRow row = t.NewRow(); foreach (var propInfo in elementType.GetProperties())
{
row[propInfo.Name] = propInfo.GetValue(item, null);
}
t.Rows.Add(row);
}
return ds;
}
So now, do I need to change this definition too. Cos, this takes IList as its parameter.
Satish Chilkury
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
public IQueryable<ConfigurationEntities> GetServiceByApplicationName(string desigName, string serviceName)
{
try
{
IQueryable lstEmpByDesig= new IQueryable; //error on this line
using (ConfigurationEntities ce = new ConfigurationEntities())
{
var query = ce.SelEmployeeByDesignationName(desigName);
lstEmpByDesig = query;
}
return lstEmpByDesig;
}
catch
{
return null;
}
}
But there is an error on IQueryable object declaration where I've mentioned //error on this line.
Satish Chilkury
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
SATISD9X
Contributor
2713 Points
487 Posts
Convert ComplexObject to IList
Apr 27, 2012 11:36 AM|LINK
I've got the resolution at the following link:
http://forums.asp.net/t/1792206.aspx/1
After which I've the following issue. FillGridView is a common function which is used by all the pages which uses GridViews.
public static void FillGridView(GridView gv, IList<SelEmployeeByDesignationName_Result> DataSource, string sortExpression, string sortDirection) { try { DataView myDataView = new DataView(); DataSet myDataSet = new DataSet(); myDataSet = DataSource.ToDataSet(); myDataView = myDataSet.Tables[0].DefaultView; if (sortExpression != string.Empty) { myDataView.Sort = string.Format("{0} {1}", sortExpression, sortDirection); } gv.DataSource = myDataView; gv.DataBind(); } catch { } }In the above code, I cannot put IList Datasource as its parameter where SelEmployeeByDesignationName_Result is of type ComplexObject of EntityFramework (EF). I need this function to take a parameter which would be common to all and convert such ComplexObject to an IList so that I can pass any ComplexObject from the page which will call this common FillGridView function.
I can provide with my code snippet if you want clear explanation. Please suggest.
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
_Manvel_
Contributor
4240 Points
922 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 12:19 PM|LINK
Well you can create base class for your entity types, so your entities will have common parient. To see how here is nice article on that topic
http://thundereagle.wordpress.com/2010/11/21/entity-framework-4-with-base-entities/
Then you can change you function to accept IQueryable<T>, where T is your base class for entities
As I understand from your code you need to parse IQueryable<T> to DataTable inside of FillGridView function. Here is another link, which descripbes how you can convert IEnumerable<T> to DataTable, which in your case might be much of help, because all you have to do, is to call ToList() on your DataSource and use the helper function
http://forums.asp.net/t/1559861.aspx/1
Hope this helps.
SATISD9X
Contributor
2713 Points
487 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 01:03 PM|LINK
Greetings ... Thanks for your reply.
To add to my earlier conversation ... this is the code for converting to ToDataset
public static DataSet ToDataSet<T>(this IList<T> list) { Type elementType = typeof(T); DataSet ds = new DataSet(); DataTable t = new DataTable(); ds.Tables.Add(t); //add a column to table for each public property on T foreach (var propInfo in elementType.GetProperties()) { t.Columns.Add(propInfo.Name, propInfo.PropertyType); } //go through each property on T and add each value to the table foreach (T item in list) { DataRow row = t.NewRow(); foreach (var propInfo in elementType.GetProperties()) { row[propInfo.Name] = propInfo.GetValue(item, null); } t.Rows.Add(row); } return ds; }So now, do I need to change this definition too. Cos, this takes IList as its parameter.
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
_Manvel_
Contributor
4240 Points
922 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 01:30 PM|LINK
Yeah but changes are minor. Just change IList<T> to IQueryable<T> in extension method's parameters definition.
SATISD9X
Contributor
2713 Points
487 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 02:48 PM|LINK
I've change the ToDataSet definition signatures:
But, what would be the FillGridView definition signatures will change to:
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
_Manvel_
Contributor
4240 Points
922 Posts
Re: Convert ComplexObject to IList
Apr 27, 2012 05:20 PM|LINK
FillGridView(GridView gv, IQueryable<BaseEntity> DataSource, string sortExpression, string sortDirection)
2. You can define this as Generic method, by changing IQueryable<BaseEntity> to IQueryable<T>, and set constraint like this
FillGridView(GridView gv, IQueryable<T> DataSource, string sortExpression, string sortDirection) where T: BaseEntity
SATISD9X
Contributor
2713 Points
487 Posts
Re: Convert ComplexObject to IList
Apr 28, 2012 12:08 PM|LINK
Thanks Manvel. Sorry for late reply.
But now, my FillGridView function caller which is:
gives me error as the GetEmployeeByDesignationName definition has the following code:
public List<SelEmployeeByDesignationName_Result> GetEmployeeByDesignationName(string desigName, string serviceName) { try { List<SelEmployeeByDesignationName_Result> lstEmpByDesig= new List<SelEmployeeByDesignationName_Result>(); using (ConfigurationEntities ce = new ConfigurationEntities()) { var query = ce.SelEmployeeByDesignationName(desigName); lstEmpByDesig = query.ToList(); } return lstEmpByDesig; } catch { return null; } }For the above function what will be the return type instead of List<SelEmployeeByDesignationName_Result> where it is expecting IQuerable?
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
_Manvel_
Contributor
4240 Points
922 Posts
Re: Convert ComplexObject to IList
Apr 28, 2012 04:31 PM|LINK
Everywhere you're using functions that return IList or IEnumerable, you need to change it to IQueryable, and in your example,
Don't call ToList() to keep it Queryable.
SATISD9X
Contributor
2713 Points
487 Posts
Re: Convert ComplexObject to IList
Apr 28, 2012 06:21 PM|LINK
I tried changing my above code to :
public IQueryable<ConfigurationEntities> GetServiceByApplicationName(string desigName, string serviceName) { try { IQueryable lstEmpByDesig= new IQueryable; //error on this line using (ConfigurationEntities ce = new ConfigurationEntities()) { var query = ce.SelEmployeeByDesignationName(desigName); lstEmpByDesig = query; } return lstEmpByDesig; } catch { return null; } }But there is an error on IQueryable object declaration where I've mentioned //error on this line.
MCTS .Net Framework 2.0 Web Applications
Web Developer 3.5
~ Please Mark as Answer if it solves your query ~
_Manvel_
Contributor
4240 Points
922 Posts
Re: Convert ComplexObject to IList
Apr 28, 2012 07:45 PM|LINK
public IQueryable<ConfigurationEntities> GetServiceByApplicationName(string desigName, string serviceName) { try { using (ConfigurationEntities ce = new ConfigurationEntities()) { return ce.SelEmployeeByDesignationName(desigName); } } catch { return null; } }