0

I am looking for a LINQ to use with my datatable and display all the duplicate rows based on certain columns in windows form datagrid view.

The LINQ should give a result similar to the plain old below give SQL.

SELECT *
FROM Table1 t
JOIN (  SELECT Taskid, Studentid, DateChangeid
      FROM Table1
      GROUP BY Taskid, studentid, datechangeid
      HAVING COUNT(*) > 1) temp
  ON t.taskid = temp.taskid AND t.studentid= temp.studentid AND t.datechangeid= temp.datechangeid
2
  • Please never just dump SQL and ask for conversion. At least show a class model so navigation properties and the multiplicity of associations are visible. Also, tell what type of LINQ you're targeting (to entities?), and show your own first efforts. They clarify more to us than you might think. Commented Feb 5, 2016 at 19:36
  • Noted, thanks. I am very new to C#, LiNQ. I will post detailed case for any further queries. Just added my code used for a similar situation. Commented Feb 12, 2016 at 18:35

2 Answers 2

1

This should do what you want:

var result = 
 t.GroupBy(g => new {TaskId = Taskid, StudentId = studentid, DatechangeId = datechangeid})
  .Where(g => g.Count > 1)
  .ToList();

Now you're joining Table1 on Table1 which isn't needed, just do the filtering on Table1 only.

Now the result will be of type List<IGrouping<'a,Table1>> though because of the anonymous keyselector, you can also define TaskId , StudentId and DateChangeID in a class to get rid of that. For example:

public class Table1GroupKey
{
   public int TaskId {get; set;}
   public int StudentId {get; set;}
   public int DateChangeId {get; set;}
}

Then you can use:

GroupBy(g => new Table1GroupKey { ... })

And your result will be List<IGrouping<Table1GroupKey,Table1>>.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, This is listing only 3 columns used for grouping. in my case I want to get complete rows with all columns from Table1. There are more columns in in my table other than the matching columns.
@user3658516 hence the IGrouping. They will be grouped by Table1GroupKey and for every key there is a list of records of type Table1 so you do have all the columns
0

I managed it by writing two linq as given below.

var RepeatedValues = from d in DataTableitems.AsEnumerable()
            group d by d.Field<string>("MobileNo") into itemGroup
            where itemGroup.Count() > 1
            select new { name = itemGroup.Key };

var RepeatedInRows= from d in DataTableitems.AsEnumerable()
              join c in RepeatedValues.AsEnumerable() on d.Field<string>
              ("MobileNo") equals c.name
              select d;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.