0

I am trying to filter my linq query, using distinct() method but I keep getting all the data records (including duplication). I have tried the following variations, which all seem to be failing.

           int total = Data.Count();

           // Data = Data.GroupBy(member => member.Tag).Select(x => x.OrderBy(y => y.Name).First());

           // Data = Data.OrderByDescending(c => c.UploadDate);

            Data = Data.Distinct().OrderBy(value => value.Tag);

            var data = Data.ToList();

How can I filter my query by showing all the data fieldnames which are filtered by unique tags field name? My tag fieldname does contain NULL data as well.

Here is my entire method, for further reference:

        [Authorize]
    [HttpPost]
   private HttpResponseMessage method(HttpContext request, Query query)
    {
        if (User.IsInRole("admin") || User.IsInRole("art"))
        {
            IQueryable<database_B> Data = null;

            if (!string.IsNullOrEmpty(query.name))
            {
                var ids = query.name.Split(',');

               // var dataMatchingTags = db.database_B.Where(c => ids.Any(id => c.Name.Contains(id)));

                if (Data == null)
                    Data = dataMatchingTags;
                else
                    Data = Data.Union(dataMatchingTags);
            }

            if (Data == null) // If no tags or name is being queried, apply filters to the whole set of products
                Data = db.database_B;

            if (query.endDate != null)
            {
                Data = Data.Where(c => c.UploadDate <= query.endDate);
            }

            if (query.startDate != null)
            {
                Data = Data.Where(c => c.UploadDate >= query.startDate);
            }

            int total = Data.Count();

           // Data = Data.GroupBy(member => member.Tag).Select(x => x.OrderBy(y => y.Name).First());

           // Data = Data.OrderByDescending(c => c.UploadDate);

            Data = Data.Distinct().OrderBy(value => value.Tag);

            var data = Data.ToList();

            if (!data.Any())
            {
                var message = string.Format("No data found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }

           // return Request.CreateResponse(HttpStatusCode.OK, data);
            return Request.CreateResponse(HttpStatusCode.OK, new { total, data });
        }

Thank you for any further help.

user3070072
  • 610
  • 14
  • 37

3 Answers3

1

You need something like that ? http://www.codeproject.com/Articles/535374/DistinctBy-in-Linq-Find-Distinct-object-by-Propert

Cokoyan
  • 79
  • 4
0

If database_B is a class (as opposed to a struct) which does not implement IEquatable<database_B> in a suitable way, Distinct will treat different objects as different, regardless of member values. A possible solution would be to implementet IEquatable<database_B> to reflect the comparision which is desired for equality.

Alternatively, a different overload of Distinct can be used, where it is possible to give a custom comparision as an argument.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

Your class database_B has to implement Equals- and GetHashCode-Method in order to tell the Distinct under which circumstances two instances are considered equal and may therefor be filtered out.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111