How to query names from a record with multiple IDs in LINQ - 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
}

Related

How to create an Oracle One-to-Many Statistics View?

I have three tables need to join statistics.The A table corresponds to multiple rows in the B table, and B has a state. I need to count the number of scrapped statuses in B and some attributes of the rows in non-rejected state, and finally grouped by A's barcode, but I don't know how to query B in different states in a query and count them in an A. I tried following codes, but nothing queried.
select
a.barcode barcode
, count(b.id) effectiveNum
, count(b2.id) scraps
, sum(b.x * c.y)
from A a
, B b
, B b2
, C c
where a.id = b.Aid
and a.id = b2.Aid
and b2.state = -1000
and b.id = c.Bid
group by a.barcode
Who can help me please?
I think you need to LEFT-JOIN in order to accept empty Lists in B. But as APC said, some example-data would be great.
SELECT a.barcode barcode,
COUNT (b.id) effectiveNum,
COUNT (b2.id) scraps,
SUM (b.x * c.y)
FROM A a
LEFT JOIN B b ON a.id = b.Ai
LEFT JOIN B b2 ON a.id = b2.Aid AND b2.state = -1000
LEFT JOIN C c ON b.id = c.Bid
WHERE 1 = 1 -- Filter A here if needed
GROUP BY a.barcode

How can I solve this CRUD set_relation_n_n issue

I have three tables:
aauth_user: id,email,pass,name,banned,last_login
aauth_user_to_groups: user_id,group_id
aauth_groups: id,name,definition
Here I want to set relation table auth_user_to_groups with auth_groups.
Here auth_user is basic table and auth_user_to_groups is relational table and auth_groups is selection table
try this:
SELECT *
FROM aauth_user_to_groups a
JOIN aauth_user b ON a.user_id = b.id
JOIN aauth_groups c ON a.group_id = c.id ;

LINQ query with Joins and aggregate

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();

Linq subtraction sum(values) from two tables

I have two db.Table1 and db.Table2 with columns named IdUser,Value
I think I should have some join but i miss the logic
it's just a logic it's not a code
how can do something like :
var total = Sum(db.Table1(Sum(Value))-db.Table2(Sum(Value))
.Where(db.Table1.IdUser=db.Table2.IdUser)
Join the tables and group
var total = from table1record in Table1
join table2record in Table2 on table1Record.IdUser equals table2Record.IdUser
group new { table1record,table2record } by table1record.IdUser into groupedRecords
select groupedRecords.Sum(x=>x.Table1Value) - groupedRecords.Sum(x=>x.Table2Value);

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