【发布时间】:2015-03-13 17:11:56
【问题描述】:
我最初(错误地)将其发布在 codereview 网站上。
我正在构建一个 WCF 服务来检索“AttributeName,AttributeGroup,AttributeValue”列表
例如,
AttributeName AttributeGroup AttributeValue
Document Type Information Systems User Guide
Document Type Information Systems Installation Instructions
Document Type Information Systems Policy
Document Type Finance Financial Statements
Application null ECM
Application null HR
Application null eData
Interest Survey 1 - Sign Me Up
Interest Survey 2 - Very Interested
Interest Survey 3 - Would Try
Interest Survey 4 - Would Watch
Interest Survey 5 - Not at all
到目前为止,一切都很好。我希望 WCF 服务返回一个强类型对象:
[DataContract]
public class Categories
{
[DataMember]
public Dictionary<string, Category> CategoryList { get; set; }
public Categories ()
{
CategoryList = new Dictionary<string, Category>();
}
}
public class Category
{
public string CategoryName { get; set; }
public CategoryLookupValues LookupValues {get;set;}
public Category(string categoryName,CategoryLookupValues values)
{
CategoryName = categoryName;
LookupValues = values;
}
}
public class CategoryLookupValues
{
[DataMember]
public string AttributeName { get; set; }
[DataMember]
public Dictionary<string,string> AttributeValues { get; set; }
public CategoryLookupValues(string attributeName,ILookup<string,string> values)
{
AttributeName = attributeName;
AttributeValues = AttributeValues;
}
}
(不要在我的合同上挑剔,仍然只是想弄清楚如何填充对象):)
我正在使用 EF 从数据库中获取数据,并且我正在尝试使用 linq 来填充对象...我已经经历了几次迭代,只是无法解决问题。
CategoryName 属性是作为参数传入的,不是结果的一部分。
我在搞砸以下内容:
var lookup = (
from attributeGroup in model.ECMGeneralLookups
from attributeValue in model.ECMGeneralLookups
where attributeGroup.AttributeName.Equals("DocType")
select new{attributeGroup,attributeValue}
).ToLookup(x=>x.attributeValue,x=>x.attributeGroup);
(ECMGeneralLookups) 是帖子开头的三列表格。
【问题讨论】:
-
我粘贴了表的结构 (AttributeName,AttributeGroup,AttributeValue) 我还粘贴了我试图将我的数据库结果转换为的对象结构 (Categories, Category,CategoryLookupValues) 我相信我需要什么是 Dictionary
>> AttributeName >
标签: c# linq entity-framework wcf linq-to-objects