在LINQ to Entity Dynamic中显示自定义字段选择

时间:2017-03-14 00:02:22

标签: c# vb.net entity-framework linq dynamic-linq

假设我有一个实体Container,它有四个属性:ContainerID(整数),ContainerNumber(字符串),Title(字符串)和{{1} }(日期)。

我有一个名为Date的第二个实体,它有三个属性:ContainerCustomFields(整数),CustomFieldID(整数和外键),ContainerID(字符串),{ {1}}(字符串)。 FieldName实体存储FieldContent实体的用户定义的自定义字段。数据如下所示:

对于容器:

ContainerCustomFields

自定义字段:

Container

是否可以返回数据,如下所示:

Container
---------
ID  |     Number     |     Title     |    Date      |
=====================================================
1   |      10000     |   1st Title   |   1/12/2017  |
2   |      10543     |   2nd Title   |   1/31/2017  |
3   |      10667     |   3rd Title   |   4/12/2016  |
4   |      12889     |   4th Title   |   5/23/2012  |

我能够获得每个容器的自定义字段及其值的列表,以及使用select语句从实体中选择我想要的列。我如何结合这两个陈述?

ID  |   ContainerID   |   FieldName   |    Content  |
=====================================================
1   |        1        |    Colour     |    Blue     |
2   |        1        |    Height     |    5000     |
3   |        1        |    Length     |    9100     |
4   |        4        |    Colour     |    Gray     |

我正在使用Web API 2将其作为我的Web应用程序的匿名类型返回。

1 个答案:

答案 0 :(得分:0)

第一组进入每个Container,转换Fields&值在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()
};

现在找到可以出现的所有字段名称:

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

现在创建一个数据表,其中包含容器和所有可能字段的列:

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

现在,为每组数据添加一行到我们的数据表,并为每个相关字段添加值:

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

现在您可以使用数据表填写网格或其他内容。数据表中缺少的字段值将设置为空。

我用于数据库表的设置代码:

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')