1

I have two tables which, to simplify, look like this:

TheFiles

FileID | UserID
 23    |   342
 53    |   352

TheData

UserID |  DateCreated
  352  |    7/22/2014
  245  |    7/25/2014  
  589  |    7/28/2014

I'm looking to return a nullable int that represents the latest UserID from the table TheData that's not in the table TheFiles. So for example, with this sample data, the return value would be 589 because it's the latest entry in TheData and it's not in the table TheFiles.

I'm having some trouble on the where when there's no data. This is what I have:

var TheNullableInt = (from d in MyDC.TheData
                      where d.UserID doesn't exist in MyDC.TheFiles
                      orderby d.DateCreated descending
                      select d.UserID).FirstOrDefault();

I'm using linq-to-SQL. How can I do the where clause?

0

2 Answers 2

1
from d in MyDC.TheData
     where  !MyDC.TheFiles.Any(tf=>tf.UserID == d.UserID)

or do a join

from d in MyDC.TheData
join tf in MyDC.TheFiles on tf.UserID equals tf.UserID into j
from x in j.DefaultIfEmpty()
where x == null
select.....
Sign up to request clarification or add additional context in comments.

1 Comment

@frenchie, not me, there are too many downvoters out there! I just upvoted it!
1
var lastest = MyDC.TheData.Where(d => !MyDC.TheFiles.Any(f => f.UserID == d.UserID))
                          .OrderByDescending()
                          .FirstOrDefault();

Or if you really want to use LINQ

var latest = (from d in MyDC.TheData
              where !MyDC.TheFiles.Any(f => f.UserID == d.UserID)
              orderby d.DateCreated descending
              select d
             ).FirstOrDefault();

2 Comments

Great answer; ADNet beat you by just 1 min. Upvoted nonetheless.
@frenchie I had actually answered first but I misread the question initially. I thought you had a where clause but you just wanted to handle scenarios where there was no records....my edit meant I re-posted late.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.