0

I have custom list. with calculated column Department Group.

Department group column takes first left letter of title columns in uppercase.

enter image description here

using SPListItemCollection I'm able to get all items. but now I am trying to bind the group record in different table.

example:

         all record of Department group : A in one table
         all record of Department group : B in one table

so I think for that I need grouping for in SPListItemCollection

I am stuck here help me.

my sample code

   using (SPSite site = new SPSite(SPContext.Current.Web.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists.TryGetList(listName);

                if (list != null) { 
                SPQuery query = new SPQuery();
                query.Query = " <Where>"+
                              "<IsNotNull>"+
                              "<FieldRef Name='DepartmentGroup' />"+
                              "</IsNotNull>"+
                              "</Where>"+
                              "<OrderBy>"+
                              "<FieldRef Name='DepartmentGroup' Ascending='True' />"+
                              "</OrderBy>";

                SPListItemCollection items = list.GetItems(query);
4
  • I think GroupBy won't be helpful if you are looking for making it to different tables.
    – Gaurravs
    Commented Sep 19, 2016 at 5:11
  • How many Departments you can have? Is it fix or not sure??
    – Gaurravs
    Commented Sep 19, 2016 at 5:12
  • I have department from A To Z Commented Sep 19, 2016 at 5:46
  • so suggest better options for group by.. Commented Sep 19, 2016 at 5:48

1 Answer 1

0

You can try adding a reference to System.Linq , filter your SPListItemCollection using a Where Linq clause and bind the result to a table as desired.

Example:

var deptA = items.Cast<SPListItem>().Where(p => p.DepartmentGroup== "A")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.