score:0

Are you saying that the collection you are joining on has duplicates? Because if so, you can just group the ProductSeries collection ahead of time.

var query = from etaRecord in EtaRecord_0140

join productSeriesRecord in ProductSeries.GroupBy(series => series.ProductSeriesID).Select(seriesGroup => seriesGroup.First())
on etaRecord.ProductSeriesID equals productSeriesRecord.ProductSeriesID
into productSeriesGroup
from productSeries in productSeriesGroup.DefaultIfEmpty()

where etaRecord.State == "A"
select new { EtaRecord = etaRecord, ProductSeriesRecord = etaRecord };

query.Dump();

Now this is assuming that you are working with a static list, not a DB. If it is a DB connection then you should probably do the distinct on the results. It could be done in a similar manner after the fact.

score:1

It looks like you need grouping:

var query = from etaRecord in EtaRecord_0140

join productSeriesRecord in ProductSeries
on etaRecord.ProductSeriesID equals productSeriesRecord.ProductSeriesID
into productSeriesGroup
from productSeries in productSeriesGroup.DefaultIfEmpty()

where etaRecord.State == "A"
group productSeries by new { etaRecord.ProductSeriesId, etaRecord } into g
select new 
       { 
         EtaRecord = g.Key.etaRecord, 
         ProductSeriesRecord = g.Select(x => x).FirstOrDefault() 
        };

UPDATED FIDDLE

score:0

I would do this in a subquery:

var query = from etaRecord in EtaRecord_0140
            where etaRecord.State == "A"
            select new 
            { 
                EtaRecord = etaRecord,
                ProductSeriesRecord = 
                  (from productSeriesRecord in ProductSeries
                   where productSeriesRecord.ProductSeriesID == etaRecord.ProductSeriesID
                   select productSeriesRecord).FirstOrDefault()
            };

In LINQ to objects this may be an inefficient operation because the subquery is executed for each etaRecord, but since the whole statement is translated into SQL the query optimizer will take care of an optimized execution plan.

That's the story for LINQ-to-entities.

LINQ-to-SQL always seems to produce n + 1 queries for group joins (which is join - into) combined with FirstOrDefault(). I've tried several scenarios but I can't get it to generate one query only. The only solution I could find generating one query is:

var query = from etaRecord in EtaRecord_0140
            where etaRecord.State == "A"
            from productSeriesRecord in
                     ProductSeries
                        .Where(ps => ps.ProductSeriesID == etaRecord.ProductSeriesID)
                        .Take(1)
                        .DefaultIfEmpt()
            select new { EtaRecord = etaRecord, ProductSeriesRecord = productSeries };

So the join syntax is abandoned and in a rather contrived way the first record of ProductSeries belonging to a EtaRecord is queried.

score:0

The problem is your extra from clause:

from productSeries in productSeriesGroup.DefaultIfEmpty() 

You should ditch that, and just use:

let productSeries = productSeriesGroup.FirstOrDefault()

... or just use productSeriesGroup.FirstOrDefault() within the select clause, like this:

var query = from etaRecord in etaRecords            
            join productSeriesRecord in productSeriesRecords
            on etaRecord.ProductSeriesId equals productSeriesRecord.ProductSeriesId
            into productSeriesGroup
            select new { EtaRecord = etaRecord,
                         ProductSeriesRecord = productSeriesGroup.FirstOrDefault() };

With either change, the result is now:

Snuh 1 - null
Snuh 2 - null
Snuh 3 - null
Snuh 4 - Description A
Snuh 5 - null
Snuh 6 - Description B

I assume that's what you wanted.

score:0

You should be able to add an extra filtering step to group by EtaRecord and just select the first record per group

ie

query  = (from r in query 
         group r by r.EtaRecord.EtaId into results 
         select results.FirstOrDefault());

Related Articles