代码之家  ›  专栏  ›  技术社区  ›  Alex

Linq正在返回类名而不是值数据

  •  0
  • Alex  · 技术社区  · 16 年前

    我有一个twittercollection对象集合,保存在列表中。我通过foreach循环填充twittercollection对象(tc),然后通过linq访问它。

    我的类及其属性如下:

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

    然后我继续用一个循环来填充它,循环遍历一组中的一组正则表达式匹配:

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

    最后,我将使用LINQ查看该集合,并仅提取某些项进行显示:

        var counts = from URL in list
                     group URL by URL into g
                     orderby g.Count()
                     select new { myLink = g.Key, Count = g.Count() };
    

    所有这些的结果都是count.mylink中“twittercollection”一词的长列表,并且没有URL计数…

    我以前把这些都处理好了,然后再把它们放到一个通用列表中。现在我搬来是为了方便,它不起作用了。

    我真的很感激有人把我从这里的痛苦中解救出来!事先谢谢。

    1 回复  |  直到 16 年前
        1
  •  2
  •   itowlson    16 年前

    您的列表是类型 List<TwitterCollection> 所以 URL 变量的类型为 TwitterCollection . 所以A Twitter收藏 是在g.key中选择的(因此是mylink),并将其呈现为字符串“twittercollection”。

    将查询更改为:

    var counts = from tc in list
                 group tc by tc.origURL into g  // note by tc.origURL to extract the origURL property
                 ...
    

    (由于twittercollection包含多个URL,因此代码中不清楚要分组的URL。我以origurl为例。)

    推荐文章