当前位置:编程学习 > C#/ASP.NET >>

linq 查询语句! 有点难度!

   怎么用linq对类别分组后,然后查询每个类别的前10条记录>?? --------------------编程问答-------------------- 没人顶么?自己沙发! --------------------编程问答-------------------- from rs in (from it in table select new {列x=列1,列1,列2,列3,列4} )
.Union(from it in table select new {列x=列3,列1,列2,列3,列4}) 
group rs by rs.列x into g 

select ....  --------------------编程问答-------------------- 上面的,不太明白 --------------------编程问答-------------------- {from tab in db.tab
 group tab  by tab.sortcolumn
 select tab
}.take(10)
--------------------编程问答-------------------- var q =  (from g in
        (from c in db.Customers
         group c by c.Country)
       select new { g.Key, Count = g.Count()}).Take(10);

--------------------编程问答-------------------- 楼主说的有些不太明白,如果是查找每个分类的内容的话楼上的就可以了
如果是取出各个内容的前10条内容的集合的话,可以如下:


            var info = (from s in set
                        group s by s.ID into temp
                        select temp.Take(10)).SelectMany(f => f).ToList();

--------------------编程问答-------------------- 学习了 谢谢! --------------------编程问答-------------------- 不知道是不是要这种效果呢? 


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gv1.DataSource = GetData2();
            gv1.DataBind();

        }


        public List<Product> GetData2()
        {
            NorthwindDataContext dc = new NorthwindDataContext();
            List<Product> products = new List<Product>();
            foreach (Category c in dc.Categories)
            {
                products.AddRange(c.Products.Where<Product>(n => n.CategoryID == c.CategoryID).Take(2));
            }
            return products;
        }
    }

--------------------编程问答--------------------
引用 5 楼 wuyq11 的回复:
var q =  (from g in
        (from c in db.Customers
        group c by c.Country)
      select new { g.Key, Count = g.Count()}).Take(10);


up --------------------编程问答-------------------- 里面不是有一个函数吗,看看MSDN,肯定可以 --------------------编程问答-------------------- 只需要先分组,然后在从分组的结果里面去指定的条数即可
如下,因为读取的每个分组的结果是一个集合,所以需要用SelectMany方法

var q = DBContext.Customers.GroupBy(c => c.GroupID).Select(c => c.Take(10)).SelectMany(m => m).ToList();
--------------------编程问答-------------------- --------------------编程问答-------------------- var result = DBContext.Customers.GroupBy(b => b.Author).Select(p => new { List = p.Take(10) }); --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- from p in usersinfo group p by p.ID into g select new{count=g.count()} --------------------编程问答-------------------- 楼上已解决 --------------------编程问答--------------------
引用 6 楼 g_lbz 的回复:
楼主说的有些不太明白,如果是查找每个分类的内容的话楼上的就可以了
如果是取出各个内容的前10条内容的集合的话,可以如下:


C# code

            var info = (from s in set
                        group s by s.ID into temp
                        select t……


hao
补充:.NET技术 ,  LINQ
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,