0

Still getting used to LINQ syntax, and have come across this query that I need to create in LINQ - but not exactly sure how.

SELECT *,
   (SELECT 1 FROM Applications 
   WHERE Applications.jID = Jobs.ID 
   AND Applications.uID = @uID) AS Applied 
FROM [Jobs]

Playing in LinqPad, but the interface isn't really helping (at least with what I can see).

4

3 回答 3

2

Based on the link provided by Paul Sasik, and his advice that you're after a LEFT OUTER JOIN, this query should meet your requirements;

var query = from job in jobs
            join app in applications on job.ID equals app.jID into grouped
            from subApp in grouped.DefaultIfEmpty()
            select new { Job = job, Applied = (subApp != null) };

EDIT: To filter by user, update the query as follows;

var query = from job in jobs
    join app in
    (
        from userApp in applications where userApp.uID == uID select userApp
    ) on job.ID equals app.jID into grouped
    from subApp in grouped.DefaultIfEmpty()
    select new { Job = job, Applied = (subApp != null) };

I personally would have reverted to just using the .Where() method directly at this point, but just thought I'd keep everything consistent and continue using the query syntax.

于 2012-12-03T15:50:34.003 回答
0
var jobs = from j in this.db.Jobs
           where !j.Applications.Any(x => x.UserId == currentUserId)
           select j;
于 2012-12-03T15:32:35.917 回答
0

I recommend checking out the tool Linqer -- which converts SQL to Linq code. It's not free but there is a 10 day trial.

enter image description here

于 2012-12-03T19:12:08.850 回答