从两个expando对象中查找公共列,并将其用于LINQ中的Join条件

时间:2015-04-27 09:40:37

标签: c# linq object join dynamic

我有两个expando对象,这些对象完全是动态的。这些将在运行时生成。现在我想通过查找公共属性来加入这些对象。

请查看以下代码以获取更多信息

    List<dynamic> object1 = 
   [{id : 10, name : "sai" age : 22},
    {id : 20, name : "ram" age : 12},
    {id : 30, name : "sony" age : 42},
    {id : 40, name : "sunny" age : 25},
    {id : 50, name : "bobby" age : 29},
    {id : 60, name : "lucky" age : 18}]
    List<dynamic>object2 = 
[{id : 10, company: "xyz" salary : 52133},
{id : 30, company: "abc" salary : 12345},
{id : 40, company: "mno" salary : 26859},
{id : 60, company: "xyz" salary : 96523}]

我想基于公共密钥加入这两个对象,即; ID

你能帮我找到两个对象的共同属性并加入两个对象,结果将是

[{id : 10, name : "sai" age : 22, company: "xyz" salary : 52133},
{id : 20, name : "ram" age : 12},
{id : 30, name : "sony" age : 42, company: "abc" salary : 12345},
{id : 40, name : "sunny" age : 25, company: "mno" salary : 26859},
{id : 50, name : "bobby" age : 29},
{id : 60, name : "lucky" age : 18, company: "xyz" salary : 96523}]

提前感谢。

  • 更新:

列表结果= map(result,result1,referenceKey);

    private dynamic map(List<ExpandoObject> result, List<ExpandoObject> result1, string referenceKey)
        {
            var Result = from x in result
                         join y in result1 on ((IDictionary<string, object>)x)[referenceKey] equals ((IDictionary<string, object>)y)[referenceKey] into yg
             from y in yg.DefaultIfEmpty()
             select new { x,y};

            List<dynamic> list = new List<dynamic>();
foreach (var entry in Result)
{
    dynamic itemToAdd = new ExpandoObject();

    var expandoX = ToDynamic(entry.x);
    var dictX = expandoX as IDictionary<string, object>;
    if(dictX != null)
    {
        foreach (KeyValuePair<string, object> e in dictX)
        {
            var dictO = itemToAdd as IDictionary<string, object>;
            if(!dictO.ContainsKey(e.Key))
                dictO.Add(e.Key, e.Value);
        }
    }

    if (entry.y != null)
    {
        var expandoY = ToDynamic(entry.y);
        var dictY = expandoY as IDictionary<string, object>;
        if(dictY != null)
        {
            foreach (KeyValuePair<string, object> e in dictY)
            {
                var dictO = itemToAdd as IDictionary<string, object>;
                if(!dictO.ContainsKey(e.Key))
                    dictO.Add(e.Key, e.Value);
            }
        }
    }

    list.Add(itemToAdd);
}

return list;
        }
        public dynamic ToDynamic(object value)
        {
            IDictionary<string, object> expando = new ExpandoObject();

            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
                expando.Add(property.Name, property.GetValue(value));

            return expando as ExpandoObject;
        }

1 个答案:

答案 0 :(得分:1)

那样的东西?

__TIMESTAMP__

更新:要使用您的属性相当动态,您需要以下扩展方法:

var result = from x in object1
             join y in object2 on x.id equals y.id into yg
             from y in yg.DefaultIfEmpty()
             select new { x,y};


List<dynamic> list = new List<dynamic>();
foreach (var entry in result)
{
    if (entry.y != null)
    {
        list.Add(new {entry.x.id, entry.x.name, entry.x.age, entry.y.company, entry.y.salary });
    }
    else
    {
        list.Add(new {entry.x.id, entry.x.name, entry.x.age});                    
    }
}

然后你可以动态地将属性添加到public static dynamic ToDynamic(this object value) { IDictionary<string, object> expando = new ExpandoObject(); foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType())) expando.Add(property.Name, property.GetValue(value)); return expando as ExpandoObject; } 个动态项目中(保留linq连接):

List

所以......你必须知道的只有3件事:List<dynamic> list = new List<dynamic>(); foreach (var entry in result) { dynamic itemToAdd = new ExpandoObject(); var expandoX = Extensions.ToDynamic(entry.x); var dictX = expandoX as IDictionary<string, object>; if(dictX != null) { foreach (KeyValuePair<string, object> e in dictX) { var dictO = itemToAdd as IDictionary<string, object>; if(!dictO.ContainsKey(e.Key)) dictO.Add(e.Key, e.Value); } } if (entry.y != null) { var expandoY = Extensions.ToDynamic(entry.y); var dictY = expandoY as IDictionary<string, object>; if(dictY != null) { foreach (KeyValuePair<string, object> e in dictY) { var dictO = itemToAdd as IDictionary<string, object>; if(!dictO.ContainsKey(e.Key)) dictO.Add(e.Key, e.Value); } } } list.Add(itemToAdd); } 是你加入的关键,有两个实体id