-2

I have 2 List of dates.

List<DateTime> 1st;
List<DateTime> 2nd;

I want a count of matching dates within this 2 dates. I can use the foreach logic & get the results.

But what is the easiest way of achieving this?

Emond
  • 50,210
  • 11
  • 84
  • 115
Anup
  • 9,396
  • 16
  • 74
  • 138
  • 5
    Before asking "what is the easiest way" just show what is *your* way. You have to provide some own idea in order to get help here, We´re not doing your work. In particular you should define what you consider "easy". Only few lines of code? Without any framework-code? Without ...? – MakePeaceGreatAgain Aug 03 '17 at 07:00
  • @HimBromBeere I have already noted the `foreach` logic in the question. Few will be there who doesn't know simple 'foreach' logic, so i didn't pasted the code. – Anup Aug 03 '17 at 07:09
  • And why doesn´t this suffice your needs? It´s obviously easy (as you noted only a few don´t know the simple logic of `foreach`) and does its job. Why need another (possibly more difficult) approach? What you consider easy is purely opinionated. – MakePeaceGreatAgain Aug 03 '17 at 07:12
  • @HimBromBeere There is a big difference between easy & smart coding. I am not an expert in coding but i like to code it in a smarter way. – Anup Aug 03 '17 at 07:14
  • Making your question not a bit less opiniated, as smart is also quite fuzzy. I assume you´re looking for a linq-oneliner as V319 provided one. But that is neither "faster" nor "consumes less memory", it´s just "shorter". – MakePeaceGreatAgain Aug 03 '17 at 07:17
  • It is obvious that you don't have the answer for this question. Rather than fooling someones answer you can better find some questions which you know the answer for. This question carries the possibility for creative solutions which can help others in future. – Anup Aug 03 '17 at 07:24

1 Answers1

3
using System.Linq;
1st.Intersect(2nd).Count();

This should work.

Also you can make inner join using linq

V319
  • 150
  • 1
  • 13
  • 2
    While this is a correct answer, OP should have spent some effort coming up with their own solution first. – Tanveer Badar Aug 03 '17 at 07:01
  • Beware of comparing dates using the default IEqualityComparer... the 2 DateTime will have to be equivalent to the ten-thousandth of a second: https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx. Consider passing in your own IEqualityComparer. – Steel Nation Aug 03 '17 at 07:04
  • Not to mention this is basically a dupe of https://stackoverflow.com/questions/7187996/intersect-two-lists-in-c-sharp – Steel Nation Aug 03 '17 at 07:06