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. 坚持自学的第二天,bootstrap初入门

    前言 昨天,初步学完了jekyll目录结构与Liquid语法的应用与认识. 日志 今天刚入门,做了一个bootstrap导航栏,但是选中状态不行,找了JS中写好的API,写法与视频中讲的有点不一样,但 ...

  2. Protocol Buffers编码详解,例子,图解

    Protocol Buffers编码详解,例子,图解 本文不是让你掌握protobuf的使用,而是以超级细致的例子的方式分析protobuf的编码设计.通过此文你可以了解protobuf的数据压缩能力 ...

  3. 15个顶级Java多线程面试题及答案

    1)现在有T1.T2.T3三个线程,你怎样保证T2在T1执行完后执行,T3在T2执行完后执行? 这个线程问题通常会在第一轮或电话面试阶段被问到,目的是检测你对”join”方法是否熟悉.这个多线程问题比 ...

  4. MVC使用Google OAuth[OWIN]注意事項

    1.前提條件,申請一個client id,頁面:https://console.developers.google.com/ 2.添加連接域名,javascript那欄位為域名即可,另一欄需要加上具體 ...

  5. 转】Maven学习总结(三)——使用Maven构建项目

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4240930.html 感谢! maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使 ...

  6. Spark Streaming 架构

    图 1   Spark Streaming 架构图 组件介绍:‰ Network Input Tracker : 通 过 接 收 器 接 收 流 数 据, 并 将 流 数 据 映 射 为 输 入DSt ...

  7. Java设计模式系列之观察者模式

    观察者模式 Observer的定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 第一 ...

  8. URAL 2045 Richness of words (回文子串,贪心)

    Richness of words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/J Description For each ...

  9. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  10. Codeforces 599C Day at the Beach(想法题,排序)

    C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...