首页 新闻 会员 周边 捐助

LINQ查询结果遍历问题?

0
悬赏园豆:5 [已关闭问题] 关闭于 2009-02-22 14:45

本人初学LINQ,关于C#迭代器的概念也比较模糊。代码如下

DataClasses1DataContext db = new DataClasses1DataContext();
            var q =
                  from c in db.Customers
                  select new
                  {
                      c.CustomerID,
                      CompanyInfo = new { c.CompanyName, c.City, c.Country },
                      ContactInfo = new { c.ContactName, c.ContactTitle }
                  };
            foreach (var d in q)
            {
                //Response.Write(d.CompanyInfo);
                foreach (IEnumerable<string> str in d.CompanyInfo)
                {
                    Response.Write(str);
                }
               
            }

我是想遍历CompanyInfo 里的信息。但是这样写后总是提示:

错误 1 “AnonymousType#1”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“AnonymousType#1”类型的变量 
请问应该如何解决?还请高手指点

问题补充: 我是想遍历CompanyInfo 里的信息,请问代码应该如何写?
seek的主页 seek | 初学一级 | 园豆:140
提问于:2009-02-16 00:40
< >
分享
所有回答(4)
0

错误已经提示的很明确了,楼主何必再发贴问呢?

刘少侠 | 园豆:290 (菜鸟二级) | 2009-02-16 10:31
0

因为你的CompanyInfo 不是一个集合

钧梓昊逑 | 园豆:945 (小虾三级) | 2009-02-16 12:22
0

mark...

Jared.Nie | 园豆:1940 (小虾三级) | 2009-02-16 13:09
0
DataClasses1DataContext db = new DataClasses1DataContext();
            var q =
                  (from c in db.Customers
                  select new
                  {
                      c.CustomerID,
                      CompanyInfo = new { c.CompanyName, c.City, c.Country },
                      ContactInfo = new { c.ContactName, c.ContactTitle }
                  }).ToList();
            foreach (var d in q)
            {
                //Response.Write(d.CompanyInfo);
                foreach (IEnumerable<string> str in d.CompanyInfo)
                {
                    Response.Write(str);
                }
                
            }

这样试试

Smile丶品位 | 园豆:208 (菜鸟二级) | 2016-06-25 14:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册