0

I have to remove duplicate rows in a array using id using C#. In the array I have 3 columns.

Following is my code

[WebMethod]
public static string GetYesterdayPatientsByTime()
{
    try
    {

            BusinessLogicLayer bal = new BusinessLogicLayer();
            DataSet ds = bal.GetYesterdayPatientsByTimeBAL();
            string[] output = new string[(ds.Tables[0].Rows.Count * ds.Tables[0].Columns.Count)];

            int count = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    output[count++] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                }
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            js.MaxJsonLength = 2147483644;
            return js.Serialize(output.Distinct().ToArray());
        }
        catch (Exception)
        {
            return "";
            throw;
        }
    }
mmvsbg
  • 3,570
  • 17
  • 52
  • 73

1 Answers1

0

You can expand the Distinct() method into DistinctBy(), which will allow You to distinct by several columns - check link: LINQ's Distinct() on a particular property

Another solution that came in my mind is create ArrayList with the results. While going through whole array, You will check if the element does exist in list, and if not You will add it.

After checking MSDN - https://msdn.microsoft.com/cs-cz/library/bb348436(v=vs.110).aspx - You can find that Distinct() by default using Default IEquatable, so You can also add own Comparer to the class, which will compare based on ID.

Community
  • 1
  • 1
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47