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. [Hive-Tutorial] What Is Hive

    What Is Hive Hive is a data warehousing infrastructure based on Hadoop. Hadoop provides massive scal ...

  2. New full duplex HTTP tunnel implementation (client and server)

    https://issues.jboss.org/browse/NETTY-246?page=com.atlassian.jirafisheyeplugin:fisheye-issuepanel —— ...

  3. BAT-使用BAT生成快捷方式

    @( echo [InternetShortcut] echo URL=C:\Windows\System32\calc.exe echo IconIndex=0 echo IconFile=C:\W ...

  4. 在线的JSON formate工具

    一个非常好的json formate工具 可以很容易发现json的错误,以及对json进行格式化 https://jsonformatter.curiousconcept.com/

  5. UVa 817 According to Bartjens (暴力,DFS)

    题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子. 所有数字不能有前导0, 且式子必须是合法的. 析:这个题很明显的暴 ...

  6. SAE搭建WordPress教程 免费建WordPress博客站

    SAE搭建WordPress教程 免费建WordPress博客站 WordPress是一种使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设自己的网志.当然,用户也可以 ...

  7. 存储过程[st_MES_RptInspectShipment]

    USE [ChangHong_612]GO/****** Object: StoredProcedure [dbo].[st_MES_RptInspectShipment] Script Date: ...

  8. 在Windows7上搭建Cocos2d-x 3.2alpha0开发环境

    在windows7上搭建COCOS2D-X开发环境并不难, 但是由于框架更新过快,很多用户都有困难.我希望你们认为这个教程有用. 建议:为了避免安全相关的问题,请以管理员权限执行所有的操作,当运行命令 ...

  9. Fibonacci数列

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...

  10. Cocos2d-x——Cocos2d-x 屏幕适配新解 – 兼容与扩展【转载】

    Cocos2d-x 屏幕适配新解 – 兼容与扩展 本文出自[无间落叶](转载请保留出处):http://blog.leafsoar.com/archives/2013/05-13-08.html 在读 ...