开发者

Is it possible to fill a HashTable with multiple keys from a Linq query?

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"
      };

I'd be able to do something like this:

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... }                                    
      };

Is this possible with .NET 4.0?

EDIT: 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.


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..
                            };

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

using System.Web.Routing;

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


What you're trying to do doesn't fit with .NET generics, because the fields you're trying to read in won't always be of the same type. A typical use of generics is something like a Dictionary<string, string>, where both the types of both the key and value are known beforehand. You'd also need it nested a level deeper; if you got your example to work, it would be returning an IEnumerable<Hashtable>, which doesn't seem like it would be good to work with.

It sounds like what you really want is a full-fledged ORM, where it will return a collection of strongly-typed classes. Use Entity Framework, and it will handle creating the collection for you without having to do the manipulation you're trying to do here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜