比方说,我有一个实体Container
有四个特性:ContainerID
(整数),ContainerNumber
(字符串),Title
(字符串),并Date
(日期)。显示在LINQ自定义字段到实体动态选择
我有第二个实体,称为ContainerCustomFields
具有三个属性:CustomFieldID
(整数),ContainerID
(整数和外键),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 |
自定义字段:
ID | ContainerID | FieldName | Content |
=====================================================
1 | 1 | Colour | Blue |
2 | 1 | Height | 5000 |
3 | 1 | Length | 9100 |
4 | 4 | Colour | Gray |
是否有可能返回数据,所以它看起来是这样的:
Container
---------
ID | Number | Title | Date | Colour | Height | Length |
====================================================================================
1 | 10000 | 1st Title | 1/12/2017 | Blue | 5000 | 9100 |
2 | 10543 | 2nd Title | 1/31/2017 | | | |
3 | 10667 | 3rd Title | 4/12/2016 | | | |
4 | 12889 | 4th Title | 5/23/2012 | Gray | | |
我能够获得每个容器的自定义字段及其值的列表以及选择我想要的列从实体使用select语句。我如何结合这两个陈述?
For Each customField In _db.ContainerCustomFields
.Where(Function(cf) cf.ContainerID = newBox.ContainerID)
returnContainers.Select(Function(c) New With
{.ContainerID = c.ContainerID,
.Number = c.ContainerNumber,
.Title = c.Title,
.Date = c.Date })
我正在使用Web API 2将它作为匿名类型返回给我的Web应用程序。
为什么一定要用匿名类型? –
@DavidG我很好,无论是C#或VB。更多的人熟悉C#。 – Nse
@LeiYang我正在使用Web API 2将我的结果返回到Kendo UI网格。需要对数据进行一些处理,而不是所有的字段都是必需的。 – Nse