title: LINQ query to find Date difference?
body: <p>I am having date details which is in format <strong>(dd/mm/yyyy)</strong> like,</p>
  
  <p>Table #1</p>
  
  <pre><code>id  Name   date          User
  --------------------------------
  1   xxx  01/01/2016     user1
  2   yyy  01/02/2016     user2
  3   aaa  02/03/2016     user1
  </code></pre>
  
  <p>Table #2</p>
  
  <pre><code>id Name       date
  --------------------
   1  xxx    07/01/2016
   2  xxx    09/01/2016
   3  yyy    05/02/2016
   4. aaa    04/03/2016
  </code></pre>
  
  <p><strong>EDIT 1:</strong>
  The logic is, For the First time the xxx occured in Table2 means, it gets the date from the Table1 to compute the day difference. For the next time xxx value occured means, it get thes the date from Table2 (not table1) Because the Table2 already have the value in that table.So the day difference will compute by using Table2 alone. Same for the other values too. 
  <strong>EDIT 2:</strong>
  Actually, I need to get the date difference for the value. I have to get the date difference by using Table1 if the Name is occurred First time. And to get the Dtae difference from Table2 , If the Name is occurred before.
  I am having 'Name' field as UNIQUE
  Result should be like,</p>
  
  <pre><code> user Name       DateDifference
   ------------------------------------
   user1 xxx 6  (07/01/2016 - 01/01/2016)
   user1 xxx 2  (07/01/2016 - 09/01/2016)
   user2 yyy 4  (01/02/2016 - 05/02/2016)
   user1 aaa 2  (02/03/2016 - 04/03/2016)
  </code></pre>
  
  <p>LINQ query I have tried is,</p>
  
  <pre><code>var query = (from item1 in Table1
                  join item2 in Table1 on item1.Name equals item2.Name
                  where item2.Date &gt; item1.Date
                  join origin in Table2 on item1.Name equals origin.Name
                  where // condition
                  group new { item1,item2 } by new {  item1.Name,item1.Date } into grp
                  select new
                  {                   
  
                      DayDiff = DbFunctions.DiffDays(grp.Key.Date, grp.Min(d =&gt; d.item2.Date)),
                  }
                  ).ToList();
  </code></pre>
  
  <p>But I could not get the result expected. I am also aware of this question, <a href="https://stackoverflow.com/questions/36004210/how-to-get-the-date-difference-by-joining-two-tables">How to get the date difference by joining two tables?</a> , But finally they did not end it in proper LINQ query option. </p>
  
  <p>How my question differ from <a href="https://stackoverflow.com/questions/36004210/how-to-get-the-date-difference-by-joining-two-tables">How to get the date difference by joining two tables?</a> ?
  The answer is in SQL. I need to code it in LINQ.</p>