Displaying Custom Fields in LINQ to Entity Dynamic Select

守給你的承諾、 提交于 2019-12-09 04:11:35

First group into each Container, turning Fields & values into arrays within the grps:

var grps = from c in _db.Containers
join ccf in _db.ContainerCustomFields
    on c.ContainerID equals ccf.ContainerID
select  new 
        {ContainerId = c.ContainerID, 
         ContainerNumber= c.ContainerNumber, 
         Title = c.Title, 
         Date = c.Date, 
         FieldName = ccf.FieldName, 
         FieldContent = ccf.FieldContent}
into f
group f by f.ContainerId
into myGroup
select new
{
    ContainerId = myGroup.Key,
    ContainerNumber = myGroup.Max(d => d.ContainerNumber),
    Title = myGroup.Max(d => d.Title),
    Date = myGroup.Max(d => d.Date),
    Fields = myGroup.Select(d => d.FieldName).ToArray(),
    Values = myGroup.Select(d => d.FieldContent).ToArray()
};

Now find all of the Field Names that can appear:

var fields = from ccf in _db.ContainerCustomFields 
             group ccf by ccf.FieldName
             into grp
             select grp.Key;

Now create a datatable that contains columns for our containers and all possible fields:

DataTable dt = new DataTable();
dt.Columns.Add("ContainerId", typeof(Int32));
dt.Columns.Add("ContainerNumber", typeof(String));
dt.Columns.Add("Title", typeof(String));
dt.Columns.Add("Date", typeof(DateTime));
foreach(var fld in fields)
{
    dt.Columns.Add(fld, typeof(String));
}

Now for each group of data add a row for to our datatable and add values for each fields that are relevant:

foreach (var row in grps)
{
    DataRow dr = dt.NewRow();
    dr["ContainerId"] = row.ContainerId;
    dr["ContainerNumber"] = row.ContainerNumber;
    dr["Title"] = row.Title;
    dr["Date"] = row.Date;
    for (int i = 0; i < row.Fields.Count(); i++)
    {
        dr[row.Fields[i]] = row.Values[i];
    }
    dt.Rows.Add(dr);    
}

Now you can use the data table to fill out a grid or whatever. Missing Field Values in the data table will be set to null.

Setup code I used for the database tables:

CREATE TABLE Container
(
    ContainerID Int, 
    ContainerNumber Varchar(50), 
    Title Varchar(50), 
    [Date] Date
)

INSERT INTO Container
VALUES
(1, '10000', '1st Title', '2017-01-12'),
(2, '10543', '2nd Title', '2017-01-31'),
(3, '10667', '3rd Title', '2017-04-12'),
(4, '12889', '4th Title', '2017-05-23')


CREATE TABLE ContainerCustomFields 
(
    CustomFieldID INT, 
    ContainerID Int, 
    FieldName Varchar(50), 
    FieldContent Varchar(50)
)

INSERT INTO ContainerCustomFields
VALUES
(1,1, 'Colour', 'Blue'),
(2,1, 'Height', '5000'),
(3,1, 'length', '9100'),
(4,4, 'Colour', 'Gray')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!