none
How to convert to left outer join RRS feed

  • Question

  • I am a little bit stuck with the following query which works, but I rather like to convert it to a left outer join so I don't have to use the foreach construction.

    Ye query (it's all about chemical analysis results and please note, I stripped most of the class/interface definitions so it probably contains errors):

            public IEnumerable<IResult> GetResults(...)
            {

                List<Result> results =
                    (from x in resultsRepository
                    join y in anotherRepository on x.Id equals y.Id
                    select new Result() { Id = ..., ElementId = ...}).ToList(); //NOTE: Force Linq to execute the query

                //TODO: How to convert below to a GroupJoin???

                foreach (Result result in results)
                {
                    result.Replicates =
                        (from z in replicateRepository.
                         where z.ResultId == someResult.Id
                         select new Replicate() { ReplicateNumber = ..., Value = ... }).Cast<IQuasimemeReplicate>

                }

                return someResults.Cast<IResult>();
          }

    Class Result implements the following interface:

        public interface IResult
        {
            int Id { get; set; }
            int ElementId { get; set; }
            IEnumerable<IReplicate> Replicates { get; set; }
        }

    Class Replicate implements the following interface:

        public interface IReplicate
        {
            int ReplicateNumber { get; set; }
            string Value { get; set; }
        }

    I red of course about the 'select into' construction but I am having difficulties in finding the right select statement to fill a complete instance of a Result class with Replicate(s).

    Thanks for any help!

    Yours sincerely,

    Evert Wiesenekker

    Monday, March 2, 2009 1:26 PM

Answers

  • Evert,

    There is a lot going on in your example. I've provided the basic components of producing a left-outer-join below.

    // start query from Results table  
    from result in Results  
    // pull in records from the Replicates table, creating a temporary identifier  
    join replicate in Replicates on result equals replicate.Result into grp  
    // query the joined records, allowing for gaps  
    from replicates in grp.DefaultIfEmpty()  
    // return required data, allowing for nulls  
    select new {  
        ResultID = result.ID,  
        ReplicateID = (int?)replicates.ReplicateID  

    You will need to tweak it for your particular scenario.
    Monday, March 2, 2009 2:50 PM
    Answerer
  • Well thank you very much, this helped a lot! A little bit late my answer because I completely lost this link (forgot to track it).

    Evert Wiesenekker
    Tuesday, April 7, 2009 7:22 PM

All replies

  • Evert,

    There is a lot going on in your example. I've provided the basic components of producing a left-outer-join below.

    // start query from Results table  
    from result in Results  
    // pull in records from the Replicates table, creating a temporary identifier  
    join replicate in Replicates on result equals replicate.Result into grp  
    // query the joined records, allowing for gaps  
    from replicates in grp.DefaultIfEmpty()  
    // return required data, allowing for nulls  
    select new {  
        ResultID = result.ID,  
        ReplicateID = (int?)replicates.ReplicateID  

    You will need to tweak it for your particular scenario.
    Monday, March 2, 2009 2:50 PM
    Answerer
  • Well thank you very much, this helped a lot! A little bit late my answer because I completely lost this link (forgot to track it).

    Evert Wiesenekker
    Tuesday, April 7, 2009 7:22 PM