Get duplicates from a table using linq for entities

In my database, I got a column called hashcode. This column stores the hash codes of the images. I want to run a query that searches for duplicate hash codes using linq for entities.

I am stuck with "where clause". How to compare hash codes?

var ans = this.pe.TPicture.Where(p => this.pe.TPicture.Count(x => x.Equals(p)) > 1);
+3
source share
1 answer

You can also use countto examine the 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 is higher than the request O(n^2) but you can do it simply using the following code inO(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]);
                    }
            }
+1
source

Source: https://habr.com/ru/post/1772341/


All Articles