是否可以查询ExpandoObject的名单? [英] Is it possible to query list of ExpandoObject?

查看:90
本文介绍了是否可以查询ExpandoObject的名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否可以查询ExpandoObject定期LINQ?原因是,我有动态ExpandoObject但我需要做一些查询之前,我可以进一步通过。

I wonder if it is possible to query ExpandoObject with regular LINQ? Reason is that I have dynamic ExpandoObject but I need to do some querying before I can pass further.

它有一些属性始终保持如编号注释还以为我无法控制的一些动态属性。

It has some properties that always stay e.g. Id, Notes but also some dynamic properties that I cannot control.

下面是我对他们的名单的样子

Here is how my list of them looks like

[
  {
    "Id": 1,
    "FileId": 1,
    "Notes": "",
    "1": "12.02.1991"
  },
  {
    "Id": 2,
    "FileId": 2,
    "Notes": "",
    "1": "12.02.1991"
  }
]



代码



你可以看到我有我的静态项,然后我确保每个项目动态密钥成为该项目的属性。的在这个例子中 1 是关键, 1991年2月12日是值

Code

As you can see I have my static items and then I make sure that every item dynamic keys become that item properties. In this example 1 is key and 12.02.1991 is value

var generatedItems = new List<object>();

foreach (var item in items)
{
    var modifiedItem = new List<KeyValuePair<string, object>>
    {
        new KeyValuePair<string, object>("Id", item.Id),
        new KeyValuePair<string, object>("FileId", item.FileId),
        new KeyValuePair<string, object>("Notes", item.Notes)
    };
    modifiedItem.AddRange(item.Fields.Select(field => new KeyValuePair<string, object>(field.Key, field.Value)));

    generatedItems.Add(ConvertToExpandoObjects(modifiedItem)); // Here I construct object from key/value pairs
}

return generatedItems; // Is it possible to query this thing?



我不认为这是相关的,但这里是我的 ConvertToExpandoObjects 的功能可按

public static dynamic ConvertToExpandoObjects(IEnumerable<KeyValuePair<string, object>> pairs)
{
    IDictionary<string, object> result = new ExpandoObject();
    foreach (var pair in pairs)
        result.Add(pair.Key, pair.Value);
    return result;
}



我想简单地做类似 generatedItems.Where (X =方式> X); 但显然它给我什么工作的,因为它不知道这些对象有编号等..

I tried to simply do something like generatedItems.Where(x => x.); but obviously it gives me nothing to work on since it doesn't know that these objects have Id etc..

那么,是否可以查询它,如果这样的话,怎么样?

So is it possible to query it and if so, then how?

推荐答案

您的建议是正确的,你将能够查询使用点符号动态对象的集合。

Your suggestion is right, you would be able to query a collection of dynamic objects using the dot notation.

var ids = generatedItems.Cast<dynamic>().Select(x => x.Id);



但是,请记住,有没有类型安全在这里,正如你所说,智能感知是不使用时,由于您使用的动态对象。

However, keep in mind that there's no type safety here and, as you stated, IntelliSense is of no use, since you're using dynamic objects.

如果您的代码依赖于一个这样的对象是否具有一个可选属性(例如,有些标题,别人不要。'T),那么这将需要多一点的体力活

If your code depends on whether one of those objects have an optional property (e.g., some have "Title", others don't), then it will require a little more manual labor.

if((generatedItems as IDictionary<String, object>).ContainsKey("Title")) {

}

这篇关于是否可以查询ExpandoObject的名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆