Tag Archives: GroupBy

CAML Query Example: Including GroupBy

Just to remember how to write these things.

<Query>
          <Where>
            <And>
              <Geq>
                <FieldRef Name="myFieldName" />
                <Value Type="DateTime">
                  <Today OffsetDays="6"/>
                </Value>
              </Geq>
              <Eq>
                <FieldRef Name="myFieldName2"/>
                <Value Type="Choice">myFieldValue</Value>
              </Eq>
            </And>         
          </Where>
          <GroupBy
            Collapse ="TRUE"
            GroupLimit ="100">
            <FieldRef Name="myFieldName">
            </FieldRef>
          </GroupBy>
          <OrderBy>
            <FieldRef Name="MBLEventStart">
            </FieldRef>
          </OrderBy>
        </Query>
Advertisements

Get distinct object based on property in a collection.

This code is taken from the source further down. Just posted it here since it’s so useful.

What if want to distinct a list of Object on ONE or MORE Properties of the object?
Simple! You want to group them and pick a winner out of the group.

List<Person> distinctPeople = allPeople 
  .GroupBy(p => new {p.PersonId, p.FavoriteColor} ) 
  .Select(g => g.First()) 
  .ToList();

Source here.