views:

50

answers:

2

In my database i got a column named hashcode. this column stores the hashcodes of pictures. I want to run a query that searches for duplicate hashcodes using linq to entities.

I got stuck with the 'where clause'. How do i compare hashcodes?

var ans = this.pe.TPicture.Where(p => this.pe.TPicture.Count(x => x.Equals(p)) > 1);
+1  A: 

You can use count also for learning linq query see http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

in your case

entities.Where(p => entities.Count(x => x.Equals(p)) > 1);

the order of above query is O(n^2) but you can do this simply with bellow code in O(n log(n))

            entities.Sort();

            List<x> repeatedItems = new List<x>();

            if (entities.Count > 1)
            {
                if (entities[0].Equals(entities[1]))
                {
                    repeatedItems.Add(entities[0]);
                }
            }

            for (int i=0;i<entities.Count;i++)
            { 
                if (i < entities.Count -1)
                {
                    if (entities[i].Equals(entities[i+1]))
                    {
                        repeatedItems.Add(entities[i+1]);
                    }
            }
SaeedAlg
Does that select the full row? Because only getting the hashcode isn't what im looking for. Shouldn't it be something like "from r in this.entities.table where r.hashcode ......." ?
Yustme
@Yustme, I wrote `Equals` to generalize it you can use it as `return r.hashcode == x;`, did you left a downvote? if is so first ask why is this then do that.
SaeedAlg
you can write `entities.Where(p => entities.Count(x => x.hashcode == p.hashcode) > 1);` it was difficult?
SaeedAlg
ofc not, but it doesn't give me the full rows, or does it? when i debug, i don't see them.
Yustme
var ans = entities.Where(p => entities.Count(x => x.Equals(p)) > 1);
SaeedAlg
get you a full row
SaeedAlg
Hi, i've tried it, the ans was empty.
Yustme
@Yustme, Whould you post your code? Also make some edit on your question to removing I remove downvote, I can't role back it untill you update the question. left a comment to me.
SaeedAlg
i added the code. and what do i need to remove? and what do you mean with 'left a downvote'?
Yustme
Nothing I removed a downvote that i left for you because i think you left a downvote to me, `var ans = this.pe.TPicture.Where(p => this.pe.TPicture.Count(x => x.HashCode ==p.HashCode) > 1);` when i said use equal you should override default `equal` function, because i didn't know whats your object I say use equal. the above should work carefully if not tell me :D
SaeedAlg
ya that worked. now how can i use the result to get the data in the columns and use that? no i didn't downvote you (now i know what you mean). i'll upvote.
Yustme
use `Select` command, do `pe.TPicture.Where(p => this.pe.TPicture.Count(x => x.HashCode ==p.HashCode) > 1).Select(p => return new {p.ID, p.Value})`
SaeedAlg
hi, that worked, i had to remove the 'return' keyword for it to work. thanks!
Yustme
@Yustme, Ohhh sorry i wrote it fast and i didn't fix it, yes no return :D
SaeedAlg
ok np, btw, after running that query, how can i get all the (for example) ID's out of 'ans'?
Yustme
@Yustme, I think you want this: `var notAns = pe.TPicture.Where(p => this.pe.TPicture.Count(x => x.HashCode ==p.HashCode) <= 1).Select(p => new {p.ID, p.Value})`
SaeedAlg
A: 
var duplicates = entities.GroupBy(e => e.HashCode)
                         .Where(g => g.Count() > 1);
Timwi
Thanks, this worked
Yustme
@Timwi, Did you left a downvote to me?
SaeedAlg