0

I have the following result:

var result = (from p1 in db.Table
              select new ReportInform 
              {
                  DataValue =  p1.DataValue, 
                  SampleDate = p1.SampleDate 
              })
              .Distinct()
              .ToList();

// Next getting list of duplicate SampleDates
var duplicates = result.GroupBy(x => x.SampleDate)
                       .Where(g => g.Count() > 1)
                       .Select (x => x)
                       .ToList();


foreach (var r in result)
{
    if (duplicates.Contains(r.SampleDate)) // get error here on incompatbility 
    {
        r.SampleDate = r.SampleDate.Value.AddMilliseconds(index++);
    }
}

enter image description here

maccettura
  • 10,514
  • 3
  • 28
  • 35
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 1
    What is the goal you are trying to achieve? BTW `.Select (x => x)` is redundant – Gilad Green Oct 10 '18 at 18:58
  • 2
    Stop masking the type you dont know with `var` and you will see your issues... – maccettura Oct 10 '18 at 18:58
  • @GiladGreen For entries that are duplicates, I like to add millseconds to it so at the end there are no duplicates in the result set. – Nate Pet Oct 10 '18 at 18:59
  • I think you mean `.Select (x => x.Key)` instead of `.Select (x => x)` ? – Igor Oct 10 '18 at 19:00
  • If you do `.Distinct()` you shouldn't have duplicates (if you override `Equals` and `GetHashCode` – Gilad Green Oct 10 '18 at 19:01
  • `For entries that are duplicates, I like to add millseconds to it so at the end there are no duplicates in the result set.` <= that is not fail proof. What if there is a non duplicate that has that same value as a duplicate (ie. duplicate's value + 1 millisecond). What are you really trying to do? Why do you care if there are duplicate datetime values? – Igor Oct 10 '18 at 19:06
  • @GiladGreen even better, if result was actually HashSet, it removes the needs for Distinct and ToList, making everything faster because it does it behind the scene and no need to make a concrete list on top of the enumeration – Kevin Avignon Oct 10 '18 at 19:06
  • Use Any() instead of Contains:duplicates.Any(x => x.Key == r.SampleDate). – jdweng Oct 10 '18 at 19:11
  • @jdweng Actually, for list, any is slower than contains - https://stackoverflow.com/questions/18651940/performance-benchmarking-of-contains-exists-and-any – Kevin Avignon Oct 10 '18 at 19:13
  • Is contains() only looking at the key or the entire object? – jdweng Oct 10 '18 at 19:18
  • @giladgreen `Distinct` applies to all properties so it is possible to have duplicated dates since they may have different `DataPoint`. – CodingYoshi Oct 10 '18 at 20:43

2 Answers2

1

Cannot convert from 'System.DateTime?' to 'System.Linq.IGrouping

That error is pretty clear but may not be at a first glance. As a programmer, you need to learn how to read, understand and make sense of compiler or runtime errors.

Anyhow it is complaining that it cannot convert DateTime? to System.Linq.IGrouping<System.DateTime, ReportInForm>. Why? Because this query returns an System.Linq.IGrouping<System.DateTime, ReportInForm>

var duplicates = result.GroupBy(x => x.SampleDate)
                   .Where(g => g.Count() > 1)
                   .Select (x => x)
                   .ToList();

The GroupBy method returns IGrouping<System.DateTime, ReportInForm> which has a Key and the Key is the thing you grouped by and a list of items in that group. You are grouping by SampleDate and checking if there are more than one items in that group and then selecting the group. Thus dulplicates has a list of IGrouping<System.DateTime, ReportInForm> and you are asking the runtime to check if it contains a DateTime? and it blows up at this line:

duplicates.Contains(r.SampleDate)

One way to fix this is: What you want to do is to select the key of that group. Thus do this:

.Select (x => x.Key)
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • @Igor, your answer wasn't bad, but this answer makes it clearer the reasons behind why the OP had troubles and why this is the way to go. +1 – Kevin Avignon Oct 10 '18 at 20:52
0

If you are expecting duplicates to be of type List<DateTime?> then you meant to write this

.Select(x => x.Key)

instead of

.Select(x => x)
Igor
  • 60,821
  • 10
  • 100
  • 175