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: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());
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()
};
Source: stackoverflow.com
Related Query
- LINQ Outer Join Has Duplicates
- Left outer join using LINQ -- understanding the code
- How can I code an outer join using LINQ and EF6?
- LINQ - outer join to parent class in EF code first
- LEFT OUTER JOIN in LINQ
- How do you perform a left outer join using linq extension methods
- LINQ - Full Outer Join
- LINQ to SQL - Left Outer Join with multiple join conditions
- LINQ to SQL Left Outer Join
- LINQ to SQL multiple tables left outer join
- How to limit a LINQ left outer join to one row
- left outer join in lambda/method syntax in Linq
- Extension method for IQueryable left outer join using LINQ
- Linq - Left outer join with dot notation
- Is an outer join possible with Linq to Entity Framework
- LINQ left outer join query error: OuterApply did not have the appropriate keys
- Simple Linq query has duplicated join against same table?
- Linq left outer join with custom comparator
- *Right* outer join in LINQ
- Outer join query using LINQ to Entities
- C# Linq full outer join on repetitive values
- Linq Left Outer Join - DefaultIfEmpty Error
- LINQ Source Code Available
- Left Outer Join - LINQ to Datatable
- LINQ Method Syntax with INNER and OUTER Join
- linq to sql: specifying JOIN instead of LEFT OUTER JOIN
- EF Linq to Entities calling ToList() on entity set generates SQL command containing multiple left outer join
- How can I implement a LEFT OUTER JOIN in LINQ using lambda syntax on Entity Framework Core 2.0?
- VB.Net LINQ - left outer join between two datatables - limit to one row
- Unable to convert SQL Query to LINQ Query for Left Outer Join
More Query from same tag
- C# Sort array of objects by property name exists in the object
- Comparing two complex objects, one from DB and one from input
- Get 3 branch of each branching node
- use for loop to iterate through Linq group result set
- Simple query performance
- Dynamic LINQ query to get Field value from Database
- ASP.NET MVC: How to use linq lambda expression with a ViewModel to get data?
- array.Take(13).Skip(x) is subtracting the take
- Sorting List<Struct> items by order of int variable in each struct
- How to test that all items of list a are in list b
- GroupBy of List<double> with tolerance doesn't work
- Linq and Cross Products
- Linq to Entities delete
- Modifying the sort order of a LINQ query
- How can I get a list of CollectionBase items?
- Use the data context as a method argument in LINQPad
- Convert VBNet LiNQ to C#
- Are there any major features missing from Linq-to-Sql?
- Linq query with count (extension method)
- How to get occurrence of data and insert it to list
- Linq nested list expression
- How to check if two dictionaries contain the same values?
- LINQ EF AND VS2017
- Get item count of a list<> using Linq
- enumerable group field using Linq?
- How to put 'all' condition inside of linq joins.(Sql query prepared)
- 'IQueryable<Entity>' does not contain a definition for 'Expand'
- Distinct values from a nested list using lambda
- need help in linq
- Nullable DateTime parameter causes an exception