Friday, 25 November 2016

SpQuery : [Error] Cannot complete this action : How to add three condition in where clause?

Multiple AND nested SPQuery (CAML):

Example 1

<Where>
      <And>
         <And>
            <Eq>
               <FieldRef Name='EmployeeID' />
               <Value Type='Text'>12345</Value>
            </Eq>
            <Eq>
               <FieldRef Name='Status' />
               <Value Type='Text'>Active</Value>
            </Eq>
         </And>
         <Eq>
            <FieldRef Name='Gender' />
            <Value Type='Text'>F</Value>
         </Eq>
      </And>
   </Where>



Example 2

<And>
    <Eq>... condition...</Eq>
    <And>
        <Eq>... condition...</Eq>
        <And>
            <Eq>... condition...</Eq>
            <And>
                <Eq>... condition...</Eq>
                <Eq>... condition...</Eq>
            </And>
        </And>
    </And>
</And>


Reference: https://docs.microsoft.com/en-us/sharepoint/dev/schema/and-element-query

Thursday, 10 November 2016

How to merge duplicate rows in Data Table in c#


           DataTable dt = new DataTable();
           dt.Columns.Add("Name", typeof(string));
           dt.Columns.Add("Result", typeof(string));

           dt.Rows.Add("John", "1,2,3,4,5");
           dt.Rows.Add("Mary ", "5,6,7,8");
           dt.Rows.Add("John", "6,7,8,9");

           DataTable dtRsult = dt.Clone();
           var distinctRows = dt.DefaultView.ToTable(true, "Name").Rows.OfType<datarow>().Select(k => k[0] + "").ToArray();
           foreach (string name in distinctRows)
           {
               var rows = dt.Select("Name = '" + name + "'");
               string value = "";
               foreach (DataRow row in rows)
               {
                   value += row["Result"] + ",";
               }
               value = value.Trim(',');

               dtRsult.Rows.Add(name, value);
               value = "";

           }