Share via


Call IEnumerable to convert List to DataSet

Question

Thursday, April 12, 2012 10:17 AM

public static void FillGridView(GridView gv, Object DataSource, bool blnMakeSelection, string sortExpression, string sortDirection) 
 
        {  
            try  
            {  
                DataView myDataView = new DataView();  
                DataSet myDataSet = new DataSet();  
  
                myDataSet = ToDataSet<string>(DataSource);//how to call ToDataSet  
                myDataView = myDataSet.Tables[0].DefaultView;  
  
                if (sortExpression != string.Empty)  
                {  
                    myDataView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);  
                }  
  
                gv.DataSource = myDataView;  
                gv.DataBind();  
            }  
            catch  
            {  
  
            }  
        }

Since the sorting takes place using DataSet and I'm getting my DataSource using EntityFramework IList resultset, I've to convert that to a DataSet using the below code:

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

Now the question is ... how would you call the above ToDataSet in FillGridView at the following line:

myDataSet = ToDataSet<string>(DataSource); //The bold section is what I need

Please advise.

All replies (8)

Friday, April 13, 2012 12:19 AM âś…Answered

You are already doing a ToList() operation in GetVariableKeyByXMLTypeName so just change the signature for FillGridView to be

public static void FillGridView(GridView gv, IList<SelVariableKeyByXMLTypeName_Result>DataSource, bool blnMakeSelection, string sortExpression, string sortDirection) 

and then in the body you would have

myDataSet = DataSource.ToDataSet();


Thursday, April 12, 2012 10:23 AM

After searching a bit, I found the two following links that might be of some use to you:

Converting a List to Dataset | StackOverflow

and

Converting a List to a DataTable | StackOverflow

Both of these offer several solutions and possible answers - hopefully one of them might suit your needs. It seems the most common suggestion would be to implement an Extension method to convert your list into a DataSet, such as:

public static DataSet ToDataSet<T>(this IList<T> list)
{
     //Code
}

Thursday, April 12, 2012 10:27 AM

Thanks Rion,

I've already placed this piece of code. Please check my original post.

The only thing I need is how do I call this extension function.

Thanks again.


Thursday, April 12, 2012 7:14 PM

ToDataSet is an extension method.  Notice that the first parameter uses the 'this' keyword.  The first parameter is of type IList<T> which tells you what type this extension method can be applied to.  From Entity Framework you are going to receive either an IQueryable<T> or an IEnumerable<T> (depends on what you have done, but it wouldn't, typically, be an IList<T>).  So you will need to convert the EntityFramework result to an IList<T> before applting the extension method.   Assuming that the result from EntityFramework is called DataFromMyDatabase then you would need

myDataSet = DataFromMyDatabase.ToList().ToDataSet();

I notice that FillGridView has a parameter called DataSource of type Object.  Is this supposed to be the result from Entity Framework?  If so, having Object as the type is a bad idea.  Make the type IEnumerable<T> and change FillGridView to FillGridView<T>. 

 


Thursday, April 12, 2012 7:37 PM

Thanks Paul for explaining that out. :)

I hope that this works for you, SATISD9X. 


Thursday, April 12, 2012 11:34 PM

Thanks Paul.

Yes, you are right. I'm calling this FillGridView as follows: (Note I've removed blnMakeSelection from my original code as it was not required)

WCMControlManager.FillGridView(gvVariableKeys, 
                                  vkc.GetVariableKeyByXMLTypeName(txtXMLType.Text, null), 
                                  e.SortExpression, sortGVOrder);

And the GetVariableKeyByXMLTypeName declaration is as follows:

public List<SelVariableKeyByXMLTypeName_Result> GetVariableKeyByXMLTypeName(string xmlTypeName, string variableKeyName)
        {
            try
            {
                List<SelVariableKeyByXMLTypeName_Result> lstVarKeyByVarTyp = new List<SelVariableKeyByXMLTypeName_Result>();

                using (ConfigurationEntities ce = new ConfigurationEntities())
                {
                    var query = ce.SelVariableKeyByXMLTypeName(xmlTypeName, variableKeyName);
                    lstVarKeyByVarTyp = query.ToList();
                }
                return lstVarKeyByVarTyp;
            }
            catch
            {
                return null;
            }
        }

So what changes will take place for the above two methods to get this List converted to DataSet.

 


Friday, April 13, 2012 12:47 AM

I'll try this and let you know the results.

Thanks Paul.


Friday, April 13, 2012 12:55 AM

Thanks a TON Paul ... This is what I exactly wanted.