是否可以用Linq查询中的多个键填充HashTable? [英] Is it possible to fill a HashTable with multiple keys from a Linq query?

查看:146
本文介绍了是否可以用Linq查询中的多个键填充HashTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将SQL Server数据库中的数据捕获到DataTable中,然后使用AsEnumerable将其转换,然后使用一系列Linq语句进行分离.我的最终目标是接受那些Linq查询,并为我生成一个HashTable.因此,而不是匿名类型:

I'm grabbing data from a SQL Server database into a DataTable that I convert using AsEnumerable and then separating using a series of Linq statements. My ultimate goal is to take those Linq queries and have it generate a HashTable for me. So instead of Anonymous types:

var facebookPosts
    = from f in results.AsEnumerable()
      where f.GetInt("SocialMediaTypeID").Equals(
          (int) SocialMediaRecordType.FacebookPost)
      select new {
          Headline = f.GetString("Headline"),
          Parameters = new Parameters(f.GetString("Parameters")),
          SocialMediaUserID = f.GetLong("SocialMediaUserID"),
          SocialMediaRecordID = f.GetLong("SocialMediaRecordID"),
          GravityScore = f.GetFloat("GravityScore"),
          NewsDate = f.GetDateTime("NewsDate", false),
          Template = "Facebook"
      };

我可以做这样的事情:

var facebookPosts1
    = from f in results.AsEnumerable()
      where f.GetInt("SocialMediaTypeID").Equals(
          (int) SocialMediaRecordType.FacebookPost)
      select new Hashtable {
          { "Headline", f.GetString("Headline"),
            "SocialMediaUserID", f.GetLong("SocialMediaUserID"),
             etc... }                                    
      };

.NET 4.0是否可能?

Is this possible with .NET 4.0?

为了澄清这个问题,最终我正在用JSON返回浏览器.我的公司创建了一个从HashTable派生的JsonObject类,这是我最终用来返回Json所必须使用的类.我想做的是在结果中为每行创建一个Hashtable(或JsonObject),使用上面的示例,该表具有一个名为 Headline 的键,该键的值 f.GetString("Headline"),另一个名为 SocialMediaUserID 的键,其值为 f.GetLong("SocialMediaUserID"),依此类推. Facebookposts最终将是HashTable或JsonObject的数组或IEnumerable,每个数组中都有几个键/值对.如果我不能以这种方式做到这一点,我想我是在对 facebookPosts 的操作中陷入了我需要的适当JsonObject结构中的问题?我确实需要明确指定名称.

To clarify the question, ultimately what I'm doing with this is returning JSON to the browser. My company has created a JsonObject class that derives from a HashTable and that is what I have to use to ultimately return the Json. What I'd like to do is create one Hashtable (or JsonObject) per row in results that has, using the example above, a key named Headline with a value of f.GetString("Headline"), another key named SocialMediaUserID with a value of f.GetLong("SocialMediaUserID") and so on. Facebookposts would ultimately be an array or IEnumerable of HashTable or JsonObject with several key/value pairs in each. If I can't do it this way, I guess I'm stuck doing manipulation of facebookPosts into the proper JsonObject structure that I need? I do need to specify the names explicitly.

推荐答案

对于Hashtable类我不太了解,因为Dictionary是.NET的标准哈希表实现.像这样吗?

I don't know much about the Hashtable class, since Dictionary is the standard hashtable implementation for .NET. Something like this?

...
select new Dictionary<string, object>
                            {
                                {"Headline", f.GetString("Headline")},
                                {"SocialMediaUserID", f.GetLong("SocialMediaUserID")},
                                // etc..
                            };

或者,如果您碰巧可以访问System.Web

Or, if you happen to have access to System.Web,

using System.Web.Routing;

facebookPosts1 = facebookPosts.Select(p => new RouteValueDictionary(p)).ToList();

这篇关于是否可以用Linq查询中的多个键填充HashTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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