Home > Enterprise >  Inner joins in LINQ
Inner joins in LINQ

Time:09-17

Wonder if anyone can quickly help me transfering this SQL to Linq. I need a little help and then i can learn from this.

SELECT raoe.AreaofExpID 

     , aoe.ServiceLineID 

     , raoe.ResponseID 

  FROM AreaofExp aoe 

 INNER 

  JOIN ResponseAreaOfExp raoe 

    ON aoe.AreaofExpID = raoe.AreaofExpID 

 INNER 

  JOIN Response resp 

    ON raoe.responseid = resp.responseid 

   AND resp.segmentid = 4; 

After working on this - i have got this :

var query = from aoe in _cctDBContext.AreaOfExp
                    join raoe in _cctDBContext.ResponseAreaOfExp on aoe.AreaofExpId equals raoe.AreaofExpId
                    join resp in _cctDBContext.Response on raoe.ResponseId equals resp.ResponseId
                    select new
                    {
                        raoe.AreaofExp,
                        aoe.ServiceLineId,
                        raoe.ResponseId
                    };

and now i am getting an error : Expected contextual keyword 'on'
I believe my second join is wrong but i am not sure how.

CodePudding user response:

Here is the code that I wrote to get your query, without modification, to work:

public static class _cctDBContext
{
    public static List<AreaOfExp> AreaOfExp = new List<AreaOfExp>();
    public static List<ResponseAreaOfExp> ResponseAreaOfExp = new List<ResponseAreaOfExp>();
    public static List<Response> Response = new List<Response>();
}

public class AreaOfExp
{
    public int AreaofExpId;
    public int ServiceLineId;
}

public class ResponseAreaOfExp
{
    public int AreaofExpId;
    public int AreaofExp;
    public int ResponseId;
}

public class Response
{
    public int ResponseId;
}

In other words, your query, as posted in the question, is fine and does not contain an error.

  •  Tags:  
  • linq
  • Related