LINQ query with Joins and aggregate - linq

I would request someone to please check whether my following LINQ query with 4 tables joins and group by is logically correct? Is it the correct way write LINQ query for 4 tables join and Group by clause?
My second point:
If I comment line TotalTime = tstGroup.Sum(x=>x.Duration), it works fine.
But, If I uncomment this line it say the following exception.
A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll
What is wrong? Please comment
Tables
Table A: Id, FirstName
Table B: Id, AId(Table A FK)
Table C: Id, BId(Table B FK), Duration
Table D: Id, AId(Table A FK). Description
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { a= a, b= b, d= d} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.a.FirstName,
Description= tstGroup.Key.d.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();
Modified LINQ.
Do you feel it is correct now? Removed multiple group clauses.
tstData= (from a in _context.A
join b in _context.B on a.Id equals b.AId
join c in _context.C on b.Id equals c.BId
join d in _context.D on a.Id equals d.AId
group c by new { c.Id, c.Duration, d.Name, a.FirstName} into tstGroup
select new myobj
{
FirstName= tstGroup.Key.FirstName,
Description= tstGroup.Key.description,
TotalTime = tstGroup.Sum(x=>x.Duration),
}).ToList();

Related

How to combine 2 teradata queries into one

can someone please help me in combining these 2 Teradata queries into a single query? The tables - cdb.dim_party_doc_id, cdb.dim_doc_issuer, mdb.fp_account_entity_map do not have customer_account_number in them, so I not able to directly join all these tables in a single query directly.
Thanks a lot!!
SELECT
det.cust_id AS customer_account_number,
c.encrypt_val AS ssn_encrypted,
det.cust_first_name AS name_1,
bal.BALANCE_AMT AS principal
FROM
cdb.DIM_CUSTOMER det
INNER JOIN
cdb.fact_stored_val_acct_dly bal
ON det.cust_id = bal.customer_id AND bal.curr_cd='USD' and bal.acct_type_code='SBA'
INNER JOIN
cdb.dim_party_acct_map b
ON bal.customer_id = b.cust_id
INNER JOIN
cdb.dim_party_doc_id c
ON b.party_key = c.party_key
AND c.status = 'A'
INNER JOIN
cdb.dim_doc_issuer d
ON c.doc_issuer_id = d.doc_issuer_id
AND d.doc_type = 'TAX_ID'
AND d.doc_subtype = 'SSN'
and
SELECT
own.owner_id AS customer_account_number,
entity.entity_id AS dd_number
FROM
mdb.fp_account_owner_map own
LEFT JOIN
mdb.fp_account_entity_map entity
ON own.fp_account_id = entity.fp_account_id
WHERE
entity.entity_type in (12)
AND
own.product_id in (5501)
Below query solves my problem
SELECT
det.cust_id AS customer_account_number,
temp.direct_deposit_account_number AS account_number,
c.encrypt_val AS ssn_encrypted,
det.cust_first_name AS name_1,
bal.BALANCE_AMT AS principal
FROM
cdb.DIM_CUSTOMER det
LEFT JOIN
cdb.fact_stored_val_acct_dly bal
ON det.cust_id = bal.customer_id AND bal.curr_cd='USD' and bal.acct_type_code='SBA'
INNER JOIN
cdb.dim_party_acct_map b
ON bal.customer_id = b.cust_id
INNER JOIN
cdb.dim_party_doc_id c
ON b.party_key = c.party_key
AND c.status = 'A'
INNER JOIN
cdb.dim_doc_issuer d
ON c.doc_issuer_id = d.doc_issuer_id
AND d.doc_type = 'TAX_ID'
AND d.doc_subtype = 'SSN'
INNER JOIN
(SELECT
own.owner_id AS customer_id,
entity.entity_id AS direct_deposit_account_number
FROM mdb.fp_account_owner_map own
LEFT JOIN
mdb.fp_account_entity_map entity
ON own.fp_account_id = entity.fp_account_id
WHERE entity.entity_type in (12)
AND own.product_id in (5501)) AS temp
ON customer_account_number=temp.customer_id

How to query names from a record with multiple IDs in LINQ

I have a table [A] that has columns such as CreatedBy(ID), AuthorizedBy(ID), SentTo(ID) and I need to join them to a table [B] containing user names (UserID, FullName). How can I write a join that connects each record of table A to multiple records in table B to fill in the CreatedBy/AuthorizedBy/SentTo names using LINQ?
can give try as below , basically you have to join B with A three times
form a in A
join b in B on b.Id = a.Createdby
join b1 in B on b1.Id = a.Authrizedby
join b2 in B on b2.Id = a.SentTo
select new {
a.Id,
CreatedBy= b.FullName,
AuthorizedBy = b1.FullName,
SentTo= b2.FullName};
or
from a in A
select new {
a.ID
CreatedBy= b.FirstOrDefault(a.CreatedBy== b.Id).FullName,
AuthorizedBy = b.FirstOrDefault(a.AuthorizedBy== b.Id).FullName,
SentTo= b.FirstOrDefault(a.SentTo== b.Id).FullName
}

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.

Linq query only returning 1 row

Dim ds = From a In db.Model
Join b In db.1 On a.id Equals b.ID
Join c In db.2 On a.id Equals c.ID
Join d In db.3 On a.id Equals d.ID
Join f In db.4 On a.id Equals f.ID
Select a.id, a.Ref, a.Type, a.etc
Above is my linq query. At the moment I am only getting the first row from the db returned when there are currently 60 rows. Please can you tell me where I am going wrong and how to select all records.
Thanks in advance!
UPDATE:
When I take out all the joins like so:
Dim ds = From a In db.1, b In db.2, c In db.3, d In db.4, f In db.5
Select a.id, a.Ref, a.type, b.etc, c.etc, d.etc
I get a system.outofmemory exception!
You're only going to get a row produced when all of the joins match - in other words, when there's a row from Model with an AP, an Option, a Talk and an Invoice. My guess is that there's only one of those.
LINQ does an inner join by default. If you're looking for a left outer join (i.e. where a particular row may not have an Invoice, or a Talk etc) then you need to use a group join, usually in conjunction with DefaultIfEmpty.
I'm not particularly hot on VB syntax, but this article looks like it's what you're after.

Group by, Sum, Join in Linq

I have this simple sql query:
select c.LastName, Sum(b.Debit)- Sum(b.Credit) as OpenBalance from Balance as b
inner join Job as j on (b.Job = j.ID)
inner join Client as c on (j.Client = c.ID)
Group By c.LastName
and I am trying to convert it to work in linq like this:
from b in Balance
join j in Job on b.Job equals j.ID
join c in Client on j.Client equals c.ID
group b by new { c.LastName } into g
select new {
Name = c.Lastname,
OpenBalance = g.Sum(t1 => t1.Credit)
}
but when I try to run it in LINQPad I get the following message:
The name 'c' does not exist in the
current context
and it highlights c.Lastname in select new statement.
Any help on this will be greatly appreciated.
Thank you.
Well, you've grouped b by c.LastName. So after the grouping operation, you're dealing with g which is a grouping with the element type being the type of b, and the key type being the type of c.LastName. It could well be that all you need is:
select new {
Name = g.Key,
OpenBalance = g.Sum(t1 => t1.Credit)
}
... but if you need to get at any other aspects of c, you'll need to change your grouping expression.

Resources