【问题标题】:LINQ to Entities - Initializing complex objects (Dictionary or List<KeyValuePair>)LINQ to Entities - 初始化复杂对象(字典或 List<KeyValuePair>)
【发布时间】:2017-05-25 11:30:11
【问题描述】:

我需要在树上表示数据,每个节点都有id、text 和几个attributes。

我已尝试保持我的 attributes 属性通用,并使用 LINQ to Entities 对其进行初始化。


尝试 1:

我的班级为:

public class TreeNode
{
    public int id { get; set; }
    public string text { get; set; }
    public Dictionary<string, object> attributes { get; set; }
    public int? parent { get; set; }
}

我尝试初始化:

List<TreeNode> taskItems = new List<TreeNode>();
using (var database = new MyDatabase())
{
    taskItems =
    (
        from t in database.Task
        select new TreeNode()
        {
            id = t.ID,
            text = t.Name,
            attributes = new Dictionary<string, object>()
            {
                { "Color", t.Color },
                { "Category", t.Category }
            },
            parent = t.ParentID
        }
    )
    .ToList();
}

上述失败并出现错误

LINQ to Entities 仅支持具有单个元素的列表初始化项。


尝试 2:

我试过List&lt;KeyValuePair&gt;而不是Dictionary:

public class TreeNode
{
    public int id { get; set; }
    public string text { get; set; }
    public List<KeyValuePair<string, object>> attributes { get; set; }
    public int? parent { get; set; }
}

我尝试初始化:

List<TreeNode> taskItems = new List<TreeNode>();
using (var database = new MyDatabase())
{
    taskItems =
    (
        from t in database.Task
        select new TreeNode()
        {
            id = t.ID,
            text = t.Name,
            attributes = new List<KeyValuePair<string, object>>()
            {
                new KeyValuePair<string, object>("Color", t.Color),
                new KeyValuePair<string, object>("Category", t.Category)
            },
            parent = t.ParentID
        }
    )
    .ToList();
}

这次上述失败并出现错误

LINQ to Entities 仅支持无参数构造函数和初始化程序。


如何实现attributes属性的初始化?

【问题讨论】:

标签: c# .net entity-framework linq


【解决方案1】:
database.Task
.Select(t=> new 
{
            t.ID,
            t.Name,
            t.Color,
            t.Category,
            t.ParentID
}).AsEnumerable()
.Select(t=> new TreeNode
        {
            id = t.ID,
            text = t.Name,
            attributes = new List<KeyValuePair<string, object>>
            {
                new KeyValuePair<string, object>("Color", t.Color),
                new KeyValuePair<string, object>("Category", t.Category)
            },
            parent = t.ParentID
        }).ToList()

【讨论】:

  • 这行得通。我已经发布了我的工作代码作为替代语法。
  • 问题是:如果你只有两种类型的属性,在这种情况下是颜色和类别;定义List的目的是什么?只需像我一样将两个属性定义为颜色和类别。我不明白你为什么使用字典或列表。
  • 以上代码是我的生产代码的简化版,属性属性比较多。
【解决方案2】:

KeyValuePair 是不可变的。创建实例后,您无法更改他的值。 创建自定义类:

class KeyValuePairCustom<T,S> 
{
    public KeyValuePairCustom() {}

    public T Key {get;set;}
    public S Value {get;set;}
}

比在您的 linq 语句中使用以下代码:

List<TreeNode> taskItems = new List<TreeNode>();
using (var database = new MyDatabase())
{
    taskItems =
    (
        from t in database.Task
        select new TreeNode()
        {
            id = t.ID,
            text = t.Name,
            attributes = new List<KeyValuePairCustom<string, object>>()
            {
                new KeyValuePairCustom<string, object>() { 
                    Key="Color", 
                    Value = t.Color
                },
                new KeyValuePair<string, object>() {
                    Key = "Category", 
                    Value = t.Category
                }
            },
            parent = t.ParentID
        }
    )
    .ToList();
}

【讨论】:

    【解决方案3】:

    @ArgeKumandan 的解决方案有效。

    这是一种替代语法,使用Dictionary:

    List<TreeNode> taskItems = new List<TreeNode>();
    using (var database = new MyDatabase())
    {
        taskItems =
        (
            from t in database.Task
            select new
            {
                id = t.ID,
                text = t.Name,
                color = t.Color,
                category = t.Category,
                parent = t.ParentID
            }
        )
        .AsEnumerable()
        .Select(t => new TreeNode()
        {
            id = t.id,
            text = t.text,
            attributes = new Dictionary<string, object>()
            {
                { "color", t.color },
                { "category", t.category }
            },
            parent = t.parent
        })
        .ToList();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多