score:5
There are the 101 Linq Samples - with two union samples Union1 and Union2
This Linq statement should get you the same results as your SQL: (it has for me on a test record-set)
var results = (from a in (from d in DiscountPromotions
group d by d.BarCode into g
select new {
BarCode = g.Key,
AmountTaken = g.Sum(p => p.AmountTaken)
}).Union(from i in ItemSaleTransactions
group i by i.BarCode into o
select new {
BarCode = o.Key,
AmountTaken = o.Sum(i => i.AmountTaken)
}) group a by a.BarCode into b
select new {
BarCode = b.Key,
AmountTaken = b.Sum(c => c.AmountTaken)
});
score:12
Here's an example of a generic union, without regard to the scenario you posted:
var something =
(from e in _repository
select new { e.Property1, e.Property2 }).Union(
(from e in _repository
select new { e.Property1, e.Property2 }));
score:48
Three useful Linq concepts operating on sets. Given set c
and set e
:
Concat gives you everything in c
or e
:
(From c In db.Customers Select c.Phone).Concat( _
From c In db.Customers Select c.Fax).Concat( _
From e In db.Employees Select e.HomePhone)
(From c In db.Customers _
Select Name = c.CompanyName, Phone = c.Phone).Concat(From e In db.Employees _
Select Name = e.FirstName & " " & e.LastName, Phone = e.HomePhone)
Union also gives you everything in c
and e
, but removes any duplicates:
(From c In db.Customers _
Select c.Country).Union(From e In db.Employees _
Select e.Country)
Except gives you everything in c
that is not in e
:
(From c In db.Customers _
Select c.Country).Except(From e In db.Employees Select e.Country)
Source: stackoverflow.com
Related Query
- how to convert sql union to linq
- How do i convert this linq code to inline sql
- How are people unit testing code that uses Linq to SQL
- How to Convert Row to Column in Linq and SQL
- How can I convert this SQL Query into LINQ (OVER (PARTITION BY Date))
- How to Convert Anonymous Type to List <dynamic> using Linq to SQL in C#
- How to code the partial extensions that Linq to SQL autogenerates?
- How to get SQL query into LINQ form in C# code
- How convert sql with count to linq
- How do I convert this SQL query to LINQ to SQL
- How to convert this SQL query to LINQ or Lambda expression?
- How might I convert this SQL code to LINQ?
- How to convert SQL to LINQ with look-ahead (lead like query)
- Does LINQ convert code to SQL queries
- How to convert this recursive code to Linq
- How can I convert this SQL to LINQ
- Linq to sql query: how to prevent duplication of code
- How to convert this complex SQL Query with Subqueries into LINQ
- How to convert this SQL to LINQ in C#?
- convert linq to object query to sql query (no linq to sql code or datacontext)
- how to convert from sql query to linq
- How to convert LINQ nested Selectmany to SQL Regular Statements
- Linq union all equivalent of sql code
- How to convert multiple SQL LEFT JOIN statement with where clause to LINQ
- How can I convert this SQL to VB.NET LINQ
- How to convert SQL query to Linq with GROUP BY on only date part of a datetime?
- How can I convert a SQL script into Linq
- How can I do an sql Union on multiple Linq IQueryables
- How do you convert SQL to LINQ
- How to convert this String-based sql query to use Linq
More Query from same tag
- Does listOfLists contain at least one list that has one or more items?
- Creating my own "LINQ" extension methods
- OrderBy Collection in Dictionary Value based on Property of the Collection
- LINQ to SQL query in C#
- Decrement all int values in Dictionary
- Select Unique Record from List based on Particular value In Linq
- EF.Property throws "The LINQ expression could not be translated"
- Linq Comparing Two Lists and Generating a New One
- Can't apply orderbydescending lambda
- Join Lambda Expression
- Is it possible to modify fields using a LINQ expression without using a SELECT?
- Concat not working - The underlying array is null
- Data Access Layer - LINQ-To-SQL and generics. Can I optimize this?
- Hiding the Constructors of LINQ to SQL DataContext
- Why is this LINQ to EF query not working?
- Linq add missing distinct values
- SQL Query continues running for a very long time if search term not found
- can't save data to database using ajax and linq
- C# Linq group rows with unique value
- Linq statement: Inner function call conflicts with enclosing method name
- How to set an asp DropDownList control's SelectMethod Respond to a selection change in another DropDownList control on the same page
- Getting error -> System.Linq.Enumerable+WhereSelectListIterator`2[Tdsb.Aris.Pims.Entity.PartnershipFunding,System.String]
- How to select both boolean values or one
- How to write this linq query to avoid too many query?
- Populate list using LINQ
- How to get type of group by query?
- IEnumerable<List<T>> to List<T>
- How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?
- How linq to nhibernate by join and Separate where in query
- LINQ, how to select distinct with two columns