LINQ is returning class name and not value data
I have a collection of TwitterCollection objects held within a List. I am populating the TwitterCollection object (tc) via a foreach loop, and then accessing it through LINQ.
My class with its properties looks like this:
//simple field definition class
public class TwitterCollection
{
public string origURL { get; set; }
public string txtDesc { get; set; }
public string imgURL { get; set; }
public string userName { get; set; }
public string createdAt { get; set; }
public string realURL { get; set; }
public string googleTitle { get; set; }
public string googleDesc { get; set; }
}
I then go on to populate it with a loop through a bunch of RegEx matches in a group:
var list = new List<TwitterCollection>();
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
var tc = new TwitterCollection
{
origURL = groups[1].Value.ToString(),
txtDesc = res.text,
imgURL = res.profile_image_url,
userName = res.from_user_id,
createdAt = res.created_at,
};
list.Add(tc);
}
Finally I am looking at the collection with LINQ and extracting only certain items for display:
var counts = from URL in list
group URL by URL into g
orderby g.Count()
select new { myLink = g.Key, Count = g.Count() };
The outcome of all this is a long list of the word "TwitterCollection" in count.myLink and no count of URLs...
I used to have this all working before I shifted to a generic List. Now I have moved for convenience, it won't work.
I'd seriously appreciate someone taking me out of my misery here! Thank开发者_如何转开发s in advance.
Your list is of type List<TwitterCollection>
, so the URL
variable is of type TwitterCollection
. So a TwitterCollection
is what gets selected into g.Key (and hence myLink), and that gets rendered as the string "TwitterCollection".
Change your query to:
var counts = from tc in list
group tc by tc.origURL into g // note by tc.origURL to extract the origURL property
...
(It's not clear from your code which URL you want to group on, as a TwitterCollection contains several URLs. I've used origURL as an example.)
精彩评论