CAML query for Group by count and data

Company Category Product Name
Microsoft Developer Visual Studio
Microsoft Consumer Windows
Microsoft Enterprise SharePoint 2010
Microsoft Mobile Windows 7
Samsung Consumer Laptops
Samsung Consumer Mobiles
Samsung Consumer Tablet
Google Consumer Search Engine
Google Consumer Google Maps

The above is my SharePoint List, I want to group by company’s name and then count the number of rows for each grouped by data and also display only chunk of data that is grouped.

Company – Microsoft – 4 count

Microsoft Developer Visual Studio
Microsoft Consumer Windows
Microsoft Enterprise SharePoint 2010
Microsoft Mobile Windows 7

Company – Samsung – 3 count

Samsung Consumer Laptops
Samsung Consumer Mobiles
Samsung Consumer Tablet

Company – Google – 2 count

Google Consumer Search Engine
Google Consumer Google Maps

My  Solution – using CAML query LINQ

  1. using (SPSite site = new SPSite(“http://server“))
  2.  
  3. {
  4.  
  5. SPWeb web = site.OpenWeb();
  6.  
  7. SPList listCAMLQuery = web.Lists["listName"];
  8.  
  9. SPQuery query = new SPQuery(); // query for all the items
  10.  
  11. DataTable dt = listCAMLQuery.GetItems(query).GetDataTable(); // get datatable for all the list items
  12.  
  13. if (dt != null && dt.Rows.Count > )
  14.  
  15. {
  16.  
  17. //Group the data
  18.  
  19. var groupedList = from row in dt.AsEnumerable()
  20.  
  21. group row by row.Field<string>(“Company”) into groupedTable
  22.  
  23. // Company is the column name for groupby
  24.  
  25. // string is the type of column
  26.  
  27. orderby groupedTable.Key // key is the groupby column category value
  28.  
  29. select new
  30.  
  31. {
  32.  
  33. Key = groupedTable.Key, // key is the groupby column category value
  34.  
  35. companyCount = groupedTable.Count(), // count for columns in a groupby
  36.  
  37. groupedRows = groupedTable.CopyToDataTable() // grouped data
  38.  
  39. };
  40.  
  41. // print result
  42.  
  43. foreach (var items in groupedList)
  44.  
  45. {
  46.  
  47. int count = items.companyCount; // count for columns in a groupby category
  48.  
  49. DataTable dt1 = items.groupedRows;
  50.  
  51. gv.DataSource = dt1; //gridview
  52.  
  53. gv.DataBind();
  54.  
  55. }
  56.  
  57. }
  58.  
  59. }
  60.  
  61. }

CAML query for Group by count and data的更多相关文章

  1. MongoDB学习笔记——聚合操作之group,distinct,count

    单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...

  2. 解决postgresql -- ERROR: 42601: query has no destination for result data

    I am learning Npgsql and PostgreSQL. I am unable to define the output parameter correctly. What am I ...

  3. mysql查询出现In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'zhibo.a.id';

    出现问题: Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: In ...

  4. mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg

    mysql5.7执行sql语句报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonagg ...

  5. Group By Count不能显示0的问题

    问题: 如对表: /*==================================================== id |score |grade ------------------- ...

  6. SQL-7查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t (group 与count)

    题目描述 查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数tCREATE TABLE `salaries` (`emp_no` int(11) NOT NULL,`salary` int ...

  7. SharePoint CAML Query小结

    CAML的结构. <View Type="HTML" Name="Summary"> <ViewBody ExpandXML="TR ...

  8. MySql 错误:In aggregated query without GROUP BY, expression #1 of SELECT list contains....

    前段时间做sql注入的时候  使用group_concat时,出现标题上的错误.经查阅一位大佬的博客,成功解决!故写此博文! 当mysql的sql_mode是only_full_group_by的时候 ...

  9. SharePoint Caml Query 使用

    需要注意的是: 再使用ListQueryWebPart部件时,默认查看的list列表会出现乱码的情况,需要开启服务器呈现模式,如图: 特此记录一下

随机推荐

  1. How to interact with the Chef Server using the Chef Server API using Shell script

    !/usr/bin/env bash   _chef_dir () { # Helper function: # Recursive function that searches for chef c ...

  2. Row Border in DataGrid 表格边框

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 第三百零五天 how can I 坚持

    今天去E世界拿抽的奖品,白去了 一趟,结果已经被拿过去了,明天还得去另一个地拿,太折腾了,今天走了1万4千多步. 哎,徐斌啊,出去见个面,这个折腾啊,受不了,想干他.开个玩笑. 黄东月,为什么打字莫名 ...

  4. linux下编译lua

    curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz 编译代码时,遇到如下错误 /usr/lib/libreadline.so: undefined r ...

  5. STL学习系列六:List容器

    List简介 list是一个双向链表容器,可高效地进行插入删除元素. list不可以随机存取元素,所以不支持at.(pos)函数与[]操作符.it++(ok), it+5(err) #include ...

  6. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  7. mysql 报错之创建自定义函数

    I experienced this error while trying to alter one of my stored procedures remotely on a master serv ...

  8. Oracle数据库如何授权收费(Database Licensing)

    Oracle软件本身是免费的,所以任何人都可以从Oracle官方网站下载并安装Oracle的数据库软件,收费的是License,即软件授权,如果数据库用于商业用途,就需要购买相应Oracle产品的Li ...

  9. javascript操作Math对象的方法总结

    //数学函数--abs 返回数字的绝对值 var a; /*a = Math.abs(-12); alert(a); //12 //数学函数--acos 返回数的反余弦数 a = Math.acos( ...

  10. 【HMTL】文字飞舞的美

    这是在一个大神那看到的就拿过来了,希望能够更多人能看到. 这个是效果: 源文件下载: 点 击 下 载