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

using (SPSite site = new SPSite(“http://server“))

{

SPWeb web = site.OpenWeb();

SPList listCAMLQuery = web.Lists["listName"];

SPQuery query = new SPQuery(); // query for all the items

DataTable dt = listCAMLQuery.GetItems(query).GetDataTable(); // get datatable for all the list items

if (dt != null && dt.Rows.Count > )

{

//Group the data

var groupedList = from row in dt.AsEnumerable()

group row by row.Field<string>(“Company”) into groupedTable

// Company is the column name for groupby

// string is the type of column

orderby groupedTable.Key // key is the groupby column category value

select new

{

Key = groupedTable.Key, // key is the groupby column category value

companyCount = groupedTable.Count(), // count for columns in a groupby

groupedRows = groupedTable.CopyToDataTable() // grouped data

};

// print result

foreach (var items in groupedList)

{

int count = items.companyCount; // count for columns in a groupby category

DataTable dt1 = items.groupedRows;

gv.DataSource = dt1; //gridview

gv.DataBind();

}

}

}

}

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. RHEL6.4 KVM 桥接上网的设置

    关闭网络管理器 chkconfig NetworkManager off  ##和桥接有冲突,要关闭 service NetworkManager stop   修改eth0为物理网口,br0为桥接网 ...

  2. Nginx和Tengine的详细安装图文教程(Linux下)

    简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...

  3. DataSnap与FireDAC三层

    相交资料: http://blog.csdn.net/shuaihj/article/details/6129131http://www.cnblogs.com/hnxxcxg/p/4007876.h ...

  4. iOS WebServiceFramework网络服务框架浅解

    网络服务几乎是每一款成功APP的必备条件,打开你手机你会发现里面不用联网的应用数量十只手指可以数出来,就算是一些以独特技术切入市场的APP如美颜相机,都至少加入了分享功能.下面我先做下简单的回顾兼扫盲 ...

  5. JSF 2 radio buttons example

    In JSF, "h:selectOneRadio" tag is used to render a set of HTML input element of type " ...

  6. [iOS UI进阶 - 4.0] 涂鸦app Demo

    A.需求 1.超简易画图,只有一种画笔 2.清屏功能 3.回退功能 4.保存功能 5.使用了cocos2D   code source: https://github.com/hellovoidwor ...

  7. [iOS UI进阶 - 0] Quiartz2D

    A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...

  8. 多浏览器兼容flv视频播放HTML

    HTML: <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http: ...

  9. PicklingError: Can't pickle <type 'generator'>: it's not found as __builtin_

    多进程传递 参数时,需要是python系统已知的,不然不知道怎么序列化

  10. PostgreSQL的 initdb 源代码分析之七

    继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging ...