c# - Find similar records in a table using LINQ -
i trying find people same name , surname in 1 table , far have written linq query. query gives me cross join result. can me remove similar set of results, please?
want show matching records in grid , user can open details of each 1 of them see if duplicates or not , take action on it
for eg
consider person's id number
current result (1,2) (1,3) (2,1) (2,3) (3,1) (3,2)
want result (1,2) (1,3) (2,3)
var query = ori in db.people dup in db.people select new duplicatedataset { activemember = ori, duplicatemember = dup }; if (!string.isnullorempty(memberfirstname)) query = query.where(w => w.activemember.givenname.trim() == memberfirstname && w.duplicatemember.givenname.trim() == memberfirstname ); if (!string.isnullorempty(membersurname)) query = query.where(w => w.activemember.surname.trim() == membersurname && w.duplicatemember.surname.trim() == membersurname); query = query.where(w => w.activemember.personid != w.duplicatemember.personid && w.activemember.memberid != w.duplicatemember.memberid); newlist = query.take(100).tolist();
here how should search people same names:
p in db.people group p new { givenname = p.givenname.trim(), surname = p.surname.trim() } g g.count() > 1 // condition filters out people without duplicates select new { name = g.key, duplicates = g.tolist() // here people same name }
you can use duplicates = g.select(p => p.personid).tolist()
if want ids of duplicated people. there not repetitions in ids. e.g. if people 1, 2, , 3 have same name john doe, get
{ name: { givenname: "john", surname: "doe" }, duplicates: [1, 2, 3] }
you can permutations these ids if need to. e.g. can use 2 loops:
private static ienumerable<tuple<t,t>> getpermutations<t>(params t[] values) { (int = 0; < values.length - 1; i++) { (int j = + 1; j < values.length; j++) yield return tuple.create(values[i], values[j]); } }
test
getpermutations(1, 2, 3) // returns (1,2) (1,3) (2,3)
note: instead of trimming names each time read them database, consider put trimmed values database.
Comments
Post a Comment