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?
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();
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.....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.