How to execute Datatable Linq Union - linq

I have a datatable that contains below data.
Table 1
880000000010747
880000000012235
880000000000010
880000000015086
880000000000028
Table 2
880000000014718
880000000014928
880000000009684
880000000013184
880000000010747
How can i union the tables and return the result below?
880000000010747
880000000012235
880000000000010
880000000015086
880000000000028
880000000014718
880000000014928
880000000009684
880000000013184

From .Net 3.5 to 4.5 (at time on writing) you can use the Union method
http://msdn.microsoft.com/en-us/library/bb386993(v=vs.110).aspx
C# Example:
var infoQuery =
(from cust in db.Customers
select cust.Country)
.Union
(from emp in db.Employees
select emp.Country)
;
VB.net Example:
Dim infoQuery = _
(From cust In db.Customers _
Select cust.Country) _
.Union _
(From emp In db.Employees _
Select emp.Country)
If you to join all regardless of duplicates check out the Concat Method:
http://code.msdn.microsoft.com/LINQ-Miscellaneous-6b72bb2a#Concat1
Hope that helps.

Related

query expressions issue in Join()

I am having a issue when writing a below expression
var contactDetails = context.contacts
.Where(s=>s.contact_code == contactCode)
.Join(context.accounts, s => s.)
It will not list down the properties after "."
Any Idea? I have import the using System.Linq too.
var contactDetails = (from con in context.contacts.Where(s=>s.contact_code == contactCode)
join acc in context.accounts
on con.Property equals acc.Property
select con or select acc or select new{con,acc})
select con if you only want contacts
select acc if you only want accounts
select new{con,acc} if you only want both
Note use only one select

Entity Framework Query select hard-coded user created column

I'm creating a select from multiple tables using a union as I need to return a list of activities that has occurred for a particular client on the database. I need to return each union with an added column so I can tell the difference between the results. If I was to do the query in SQL it would look something like this:
SELECT cn.NoteID, cn.Note, cn.InsertedDate, 'Note Added' Notes
FROM Client c
INNER JOIN ClientNotes cn ON cn.ClientID = c.ID
WHERE c.ClientID = #ClientID
UNION
SELECT rc.ID, rc.CommNote, rc.InsertedDate, 'Communication Added' Notes
FROM ReceivedCommunication rc
LEFT JOIN Job j ON j.ID = rc.JobID
WHERE j.ClientID = #ClientID or rc.ClientID = #ClientID
My Question is how in Entity Framework using IQuerable do I return the hard-coded Notes column?
I have something like this so far:
Dim client as IQueryable(Of myresultclass) =
(From c As Client
Join cn As ClientNotes In ClientCompanyNotes On c.ID Equals cn.ClientID
Where c.ClientID = ClientID
Select cn.NoteID, cn.Note, cn.InsertedDate).Union(
From rc As ReceivedCommunication In ReceivedCommunications
Join j As Job In Jobs On j.ID Equals rc.JobID
Where j.ClientID = ClientID or rc.ClientID = ClientID
Select rc.ID, rc.CommNote, rc.InsertedDate)
Thanks for your help
Ok worked it out, should have been obvious. For anyone with the same issue, I had to update my Select from Select cn.NoteID, cn.Note, cn.InsertedDate to:
Select New myresultclass With {
.ActivityID = cn.NoteID,
.ActivityType = "Note Added"
.InsertedDate = cn.InsertedDate
}
for each one of the unions that I had
Thanks

How to convert following SQL query to Lambda expression?

I have a following SQL query how can I convert to lambda expression
select * from ContractItems
where ID in (SELECT distinct contractItemId from ContractPackageItems
where contractPackageId in (SELECT ID from ContractPackage
where ContractID = 680))
from the above query I need to know if row exist or not. If row exist then return true.
-TIA
---Update---
Here is what I got but it is not working
(from contractItem in _entities.ContractItems
where contractItem.ID == (from contractPackageItems in _entities.ContractPackageItems
where contractPackageItems.ContractPackageID == (from contractPackage in _entities.ContractPackages where contractPackage.ContractID == contractId select contractPackage.ID) select contractPackageItems.ContractItemId).Distinct()).Any();
Will this not do what you want?
var results = (from ci in _entities.contractItems
join cpi in _entities.contractPackageItems on ci.ID equals cpi.contractItemId
join cp in _entities.contractPackage on cpi.contractPackageId equals cp.ID
where cp.ContractID = 680
select ci).Any();

Entity to Linq Left Join + Grouping + Sum

SQL Query (Execution plan cost = 0.0127553)
SELECT
SUM(DATEDIFF(second, DTActivate, DTDeActivate)) AS Seconds,
AA.ID AS AAID,
AA.WorkStation
FROM
DbLogItems I
INNER JOIN DbApplicationArguments AA ON
AA.Id = I.ApplicationArgument_ID
GROUP BY
AA.ID,
AA.WorkStation
C#
var q = from items in db.LogItem
join aa in db.ApplicationArguments on
items.ApplicationArgument.ID equals aa.ID
into aaGroup
from aaJoin in aaGroup.DefaultIfEmpty()
group items by new
{
aaJoin.ID,
aaJoin.WorkStation
} into grouping
select new
{
Seconds = grouping.Sum(x => SqlFunctions.DateDiff("second", x.DTActivate, x.DTDeActivate)),
grouping.Key.ID,
grouping.Key.WorkStation
};
Result SQL very big (Execution plan cost = 0.0199849)
SELECT
1 AS [C1],
[GroupBy1].[A1] AS [C2],
[GroupBy1].[K1] AS [ID],
[GroupBy1].[K2] AS [WorkStation]
FROM
(
SELECT
[Join1].[K1] AS [K1],
[Join1].[K2] AS [K2],
SUM([Join1].[A1]) AS [A1]
FROM
(
SELECT
[Extent2].[ID] AS [K1],
[Extent2].[WorkStation] AS [K2],
DATEDIFF(second, [Extent1].[DTActivate], [Extent1].[DTDeActivate]) AS [A1]
FROM
[dbo].[DbLogItems] AS [Extent1]
LEFT OUTER JOIN [dbo].[DbApplicationArguments] AS [Extent2]
ON [Extent1].[ApplicationArgument_ID] = [Extent2].[ID]
) AS [Join1]
GROUP BY
[K1],
[K2]
) AS [GroupBy1]
help please write correct linq code.
My SQL Execution plan cost = 0.0127553.
Linq SQL Execution plan cost = 0.0199849.
DIFF = 0,0072296 on 21+10 records
LINQ to SQL queries are what they are. Sometimes you can't emit better SQL. However, you can write plain old T-SQL and call it in a number ways: a table-valued UDF perhaps that your customized DataContext exposes as an IQueryable.

Join statement in Linq to Sql

I need to write Join statment after writing query in linq
example :
var Query = (from Tab in Db.Employees
select Tab)
as i have some cases to perform join operation so
i need to do it on this Query Query.Join(Join with another Table like Department); I need the Syntax
if (DeptID != -1){ Query.Join(Join with table Department where FkDeptID = DeptID); }
Consider the usage of join in the LINQ 'query syntax':
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
Something like this?
var results = (from q in Query
join m in myList on q.SomeID = m.SomeID
select unknown);
Try using this query:
var Query =
from e in Db.Employees
join d in Db.Departments on e.FkDeptID equals d.DeptID into departments
select new
{
Employee = e,
Department = departments.SingleOrDefault(),
};
This works assuming that when e.FkDeptID == -1 that there is no record in the Departments table and in that case Department would be assigned null.
You should never have more than one department for an employee so I've used SingleOrDefault rather than FirstOrDefault.

Resources