zoukankan      html  css  js  c++  java
  • CAML query for Group by count and data

    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 > 0)
    
    {
    
    //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();
    
    }
    
    }
    
    }
    
    }

  • 相关阅读:
    静态变量的问题
    水晶报表动态表数据显示的问题
    USACO Section 1.1 : Friday the Thirteenth
    USACO Section 1.2 : Transformations
    USACO Section 1.1 : Programming Contest Problem Types
    USACO Section 1.2 : Milking Cows
    USACO Section 1.1 : Greedy Gift Givers
    USACO Section 1.1 : Your Ride Is Here
    USACO Section 1.1 : Broken Necklace
    舍入Table.TransformColumns(Power Query 之 M 语言)
  • 原文地址:https://www.cnblogs.com/icedog/p/SharePoint.html
Copyright © 2011-2022 走看看