??? GetProductByGroup()
{
var q = from p in db.Products
                    group p by p.CategoryID into g
                    select new { CategoryID = g.Key, g };
return q;
}
如上面的查询,我根据分类ID:CategoryID进行分类,然后返回结果。那么这个返回值类型该写什么呢?

解决方案 »

  1.   

    查询的是一个Products表的集合,可以返回IList<Products>
      

  2.   

    不行吧
    即便返回IEnumerable非泛型,外边也无法识别里边的元素类型
      

  3.   


    Products GetProductByGroup()
    {
      Products q = from p in db.Products
                   group p by p.CategoryID into g
                   select new Products
                     {
                        CategoryID = g.Key,
                        Count=g.Count() 
                      };
      return q;
    }
      

  4.   

     返回    System.Collections.IEnumerable用的时候System.Collections.IEnumerable list;
     foreach (dynamic item in list)
     {
                        
     }但是dynamic非常损耗性能,建议先建一个实体来做组成list
    List<Product> productList=from p in db.Products
      group p by p.CategoryID into g
      select new  Product { CategoryID = g.Key, g };
    return productList;
      

  5.   

    你确定你的代码能编译通过么?
    从语义上,{ CategoryID = g.Key, g }是一个全新的匿名类,和现有的Product 是不同的。
      

  6.   

    你确定你的代码能编译通过么?
    从语义上,{ CategoryID = g.Key, g }是一个全新的匿名类,和现有的Product 是不同的。
      

  7.   

    返回的是一个IQueryable<匿名类>对象,也就是说只返回一个可查询的对象。
    你既然要这样定义一个函数,就不能使用匿名类了。
      

  8.   

    像{ CategoryID = g.Key, g }这样一个匿名类型该怎么定义呢?
      

  9.   

    而且对于数据访问层来说,返回查询结果很正常的。即使是.ToList()返回,返回的是List<???>,这问号该填什么呢?请以楼主的为例子。
      

  10.   

    匿名类不能作为函数返回值和参数。只能在函数内部使用:You cannot declare a field, a property, an event, or the return type of a method as having an anonymous type. Similarly, you cannot declare a formal parameter of a method, property, constructor, or indexer as having an anonymous type. To pass an anonymous type, or a collection that contains anonymous types, as an argument to a method, you can declare the parameter as type object. However, doing this defeats the purpose of strong typing. If you must store query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.来自msdn:
    http://msdn.microsoft.com/en-us/library/bb397696.aspx
      

  11.   

    这个东西
     select new { CategoryID = g.Key, g }
    明显就不是Products!不信,你自己编译下看。
      

  12.   

    仔细看了下,你不需要创建一个匿名类,直接返回IEnumerable<IGrouping<TKey, TSource>>即可。
    下面是我的测试代码,works well.
    假设Product定义如下:
    class Product
        {
            public int CategoryID{set;get;}
            public string Description { set; get; }
            public int ProductID { set; get; }
        }你的函数可以写成这样
    IEnumerable<IGrouping<int, Product>> GetProductByGroup(List<Product> products)
            {
                return products.GroupBy(p => p.CategoryID);
                
            }测试代码:List<Product> products = new List<Product>() { 
                    new Product(){CategoryID = 1, Description="1 description", ProductID = 1},
                    new Product(){CategoryID = 2, Description="2description", ProductID = 2},
                    new Product(){CategoryID = 2, Description="3description", ProductID = 3},
                    new Product(){CategoryID = 3, Description="4description", ProductID = 4},
                    new Product(){CategoryID = 3, Description="5description", ProductID = 5},
                    new Product(){CategoryID = 3, Description="6description", ProductID = 6},            };            foreach (IGrouping<int, Product> g in GetProductByGroup(products))
                {
                    Console.WriteLine(g.Key);
                    foreach (Product p in g)
                    {
                        Console.WriteLine(p.Description);
                    }
                }