【问题标题】:Build a collection of object using EF, C#, linq (Dictionary<string,List<string,string>>)使用 EF、C#、linq (Dictionary<string,List<string,string>>) 构建对象集合
【发布时间】: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


【解决方案1】:

很难说出你的数据库表是什么......但看起来你想要类似...... var lookup= model.ECMGeneralLookups.Where(x=>x.AttributeName=="DocType").Select(x=>new{x.attributeGroup, x.attributeValue}).FirstOrDefault();

(或者您可以将 select 替换为 ToLookup)

【讨论】:

  • 查看 ToLookup()... 看起来会很棒,但也需要帮助。我能够成功检索数据 ToLookup(),但我正在尝试与我的业务对象进行比较,以及如何填充这些数据以通过 WCF 服务返回它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
相关资源
最近更新 更多