How to make smoothing function for linq object (Linq To Entities for mysql)?

I am updating an old program and using linq for basic selection so that I can learn linq in the process. I have a recurring task to just show data from different connections - this is a grid view, Below is an example

protected void Page_Load(object sender, EventArgs e)
{
    using ( vavestockModel.vavestockEntities db = new vavestockModel.vavestockEntities())
    {
        var prod = (from p in db.products select p);
        var prodd = (from p in db.productdetails select p);
        var prode = (from p in db.product_extra_data select p);
        var join1 = (from p in prod
                     join
                         pd in prodd
                         on p.PrStyle equals pd.StyleCode
                     select new {pr=p,prd=pd }).ToList();
        var join2 = (from p in prod
                     join
                         pd in prodd
                         on p.PrStyle equals pd.StyleCode
                     select new { p.PrShow,p.PrPrice,pd.I_EAN_51,pd.I_EAN_50 }).ToList();
        var join3 = (from p in prod
                     join
                         pd in prodd
                         on p.PrStyle equals pd.StyleCode
                     select new { flattenmodel(p),flattenmodel(pd) }).ToList();

        Response.Write(join1);
        GridView1.DataSource = join1;
        GridView1.DataBind();
    }

}

??object flattenmodel(vavestockModel.vavestockEntities en)//? here i want to pass any table row
{
    string[] toren;
    //foreach (var item in en.)
    //{

    //}
    return toren;
}

join2 can be bound to gridview, and join1 can be bound to return an object object. Since I have to select all columns that re-write all names for so many tables, it is not a smart choice, so I want to write a function that returns flattened data.

But it's hard for me to continue. My difficulty comes because I do not know what should be returned and passed as parameters in this case. Can someone point me in the right direction?

+4
1

System.Reflection, , List, List .

Entities ent = new Entities();
var data = (from x in ent.Employees where x.Id == 1 select x).FirstOrDefault();
List<string> list = ConvertToList(data);

public List<string> ConvertToList(object obj)
    {
        List<string> list = new List<string>();
        PropertyInfo[] pinfo = obj.GetType().GetProperties();
        foreach (var info in pinfo)
        {
            if (info.GetValue(obj) != null)
                list.Add(info.GetValue(obj, null).ToString());
        }
        return list;
    }

Edit

DataTable .

public DataTable ConvertToDataTable(IEnumerable<dynamic> obj)
    {
        DataTable dt = new DataTable();
        foreach (dynamic item in obj)
        {
            DataRow dr = dt.NewRow();
            PropertyInfo[] pinfo = item.GetType().GetProperties();
            foreach (var info in pinfo)
            {
                if (!dt.Columns.Contains(info.Name))
                    dt.Columns.Add(info.Name, info.PropertyType);

                var val= info.GetValue(item, null);
                dr[info.Name] = val;

            }
            dt.Rows.Add(dr);
        }
        return dt;
    }

Entities ent = new Entities();
var data = (from x in ent.Employees where x.Id == 1 select x).FirstOrDefault();
DataTable dt=ConvertToDataTable(data);
+2

All Articles