How to join and select table data in mvc C#? - c#

In my project I have services.
So in side the service I want to join tables and want to select more than one table data.
So I write this cording.
var query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (opv.ceremony_id == ceremony_id) &&
(!o.Deleted) && (opv.IsHireItem == true) &&
(!p.Deleted) &&
(!pv.Deleted) && (opv.ceremony_id == ceremony_id)
select opv,g;
But there is error and I can't select opv and g. if I write select opv;it is ok. but i want to select both table.
How can i do it??

Try using anonymous types i.e.
query1 = from opv in _opvRepository.Table
join o in _orderRepository.Table on opv.OrderId equals o.Id
join g in _graduandRepository.Table on opv.graduand_id equals g.graduand_id
join pv in _productVariantRepository.Table on opv.ProductVariantId equals pv.Id
join p in _productRepository.Table on pv.ProductId equals p.Id
where (opv.ceremony_id == ceremony_id) &&
(!o.Deleted) && (opv.IsHireItem == true) &&
(!p.Deleted) &&
(!pv.Deleted) && (opv.ceremony_id == ceremony_id)
select new { table1Val = opv,
table2Val = g
};

Related

Linq query syntax to method query syntax

Can anyone help with how to transform this LINQ query syntax to method syntax.
It is because i need to use the Take() method.
IQueryable<QueueItem> rs = from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a;
You can do something like this:
IQueryable<QueueItem> rs = (from a in ldb.QueueItems
join b in ldb.Robots on a.RobotId equals b.Id
join c in ldb.RobotsXEnvironments on b.Id equals c.RobotId
join d in ldb.Environments on c.EnvironmentId equals d.Id
join e in ldb.Releases on d.Id equals e.EnvironmentId
where e.ProcessKey == dropdown || a.Reference == query ||
a.SpecificData.Contains(query) &&
a.StartProcessing >= fromDate && a.EndProcessing <= toDate
select a).take(number);

Left Join only return values where UserID match

Left join return duplicate products when 1 or more user save same product.
I solved the problem in SQL query.here's a query:
select p.ProductID,
(case when c.UserID = 3 then 'true' else 'false' end) as flag
from product as p
left join SavedItem as c on product.ProductID = c.ProductID and
c.UserID = 3
can't figure out how to do in a Entity framework.
left join SavedItem as c on product.ProductID = c.ProductID and
c.UserID = 3
Left join can be solve like this:
join c in SavedItem on p.ProductID equals c.ProductID into lj
from c in lj.DefaultIfEmpty()
c.UserID = 3 where to place this?
Try this:
var query= from product in context.Products
from SavedItem in context.SavedItems.Where(c=> c.ProductID = product.ProductID && c.UserID == 3).DefaultIfEmpty()
select new {
ProductID=product.ProductID,
Flag=(SavedItem==null || SavedItem.UserID != 3) ? false : true
};
Here! I found the solution.
from product in context.Products
join c in context.SavedItems
on new { p1 = (int?)product.ProductID , p2 = (int?)cat.UserID }
equals new { p1 = c.ProductID ,p2 = c.UserID} into lj
from c in lj.DefaultIfEmpty()
select new{
...
flag = (c.UserID == cat.UserID ? "true" : "false"),
...
}

How to left join multiple tables with LINQ

I'm trying to left join three tables with LINQ. I have the SQL working as below:
Select j.Id, u.FirstName , u.LastName, u.Role
From Job j
left join JobTranslator as jt on j.Id = jt.JobId
left join JobRevisor as jr on j.Id = jr.JobId
left join [User] as u on jt.UserId = u.Id OR jr.UserId = u.Id
Where u.Id = someID;
I can get it to work with two joins like below:
IQueryable<Job> jobs =
from j in _db.Jobs
join jr in _db.JobRevisors on j.Id equals jr.JobId into jrs
from jrResult in jrs.DefaultIfEmpty()
join u in _db.Users on jrResult.UserId equals u.Id into jrU
from jrUResult in jrU.DefaultIfEmpty()
where jrUResult.Id == userId
orderby j.Id
select j;
But when I try to join my last needed table it doesn't work like below.
IQueryable<Job> jobs =
from j in _db.Jobs
join jt in _db.JobTranslators on j.Id equals jt.JobId into jts
from jtResult in jts.DefaultIfEmpty()
join jr in _db.JobRevisors on jtResult.Id equals jr.JobId into jrs
from jrResult in jrs.DefaultIfEmpty()
join u in _db.Users on jrResult.UserId equals u.Id into jrU
from jrUResult in jrU.DefaultIfEmpty()
join u in _db.Users on jtResult.UserId equals u.Id into jtU
from jtUResult in jtU.DefaultIfEmpty()
where jtUResult.Id == userId
orderby j.Id
select j;
Any ideas from anyone?
From Linq - left join on multiple (OR) conditions :
IQueryable<Job> jobs = (from j in _db.Jobs
join jt in _db.JobTranslators on j.Id equals jt.JobId into jts
from jtResult in jts.DefaultIfEmpty()
join jr in _db.JobRevisors on jtResult.Id equals jr.JobId into jrs
from jrResult in jrs.DefaultIfEmpty()
join u in _db.Users on jtResult.UserId equals u.Id into jtU
from jtUResult in jtU.DefaultIfEmpty()
where jtUResult.Id == userId
orderby j.Id
select j).Concat(
from j in _db.Jobs
join jt in _db.JobTranslators on j.Id equals jt.JobId into jts
from jtResult in jts.DefaultIfEmpty()
join jr in _db.JobRevisors on jtResult.Id equals jr.JobId into jrs
from jrResult in jrs.DefaultIfEmpty()
join u in _db.Users on jrResult.UserId equals u.Id into jrU
from jrUResult in jrU.DefaultIfEmpty()
where jtUResult.Id == userId
orderby j.Id
select j
).Distinct()

entity framework multi column

In sqlcommand I have this:
SELECT *
FROM cliente c
LEFT JOIN abono a on
c.idcliente = a.idcliente
and (a.estatus = 1 or a.estatus = null)
LEFT JOIN usuario u on
a.creadopor = u.idusuario
WHERE c.estatus = 1
We know this is not the same than this:
SELECT *
FROM cliente c
LEFT JOIN abono a on
c.idcliente = a.idcliente
LEFT JOIN usuario u on
a.creadopor = u.idusuario
WHERE c.estatus=1
and (a.estatus = 1 or a.estatus = null)
How can I do the first query in entity framework?
the second query in entity framework is it
from c in Conexion.conexion.conect.cliente
join a in Conexion.conexion.conect.abono
on c.idcliente equals a.idcliente into alj
from a in alj.DefaultIfEmpty()
join u in Conexion.conexion.conect.usuario
on a.creadopor equals u.idusuario into ulj
from u in ulj.DefaultIfEmpty()
where c.estatus == 1
&& (a.estatus == 1 || a.estatus == null)
but i could not get the first query
This does the trick:
from c in Conexion.conexion.conect.cliente
join a in Conexion.conexion.conect.abono.Where(x.estatus == 1 || x.estatus == null)
on c.idcliente equals a.idcliente into alj
from a in alj.DefaultIfEmpty()
join u in Conexion.conexion.conect.usuario
on a.creadopor equals u.idusuario into ulj
from u in ulj.DefaultIfEmpty()
where c.estatus == 1

LINQ OrderBy Count of Records in a Joined Table

I'm having trouble translating the following tSQL to LINQ to SQL in C#. Any help would be much appreciated:
SELECT P.Name
FROM Product P
INNER JOIN OrderItems OI ON P.productID = OI.productID
INNER JOIN Orders O ON OI.orderID = O.orderId
WHERE P.Active = 1 AND O.Status > 2
ORDER BY count(OI.orderID) DESC
It's the ordering by the COUNT of a JOINED table that's throwing me for a loop.
Here's what I have so far (with no orderby):
from p in CRM.tProducts
join oi in CRM.tOrderItems on p.prodID equals oi.prodID
join o in CRM.tOrders on oi.orderID equals o.orderID
where o.status > 1 && p.active == true
select p;
Thanks for any help!
You need to execute a group by if you want the count
SELECT P.Name
FROM Product P
INNER JOIN OrderItems OI ON P.productID = OI.productID
INNER JOIN Orders O ON OI.orderID = O.orderId
WHERE P.Active = 1 AND O.Status > 2
GROUP BY P.Name
ORDER BY count(*) DESC
I'll assume you actually want the count for each group in the projection.
from p in CRM.tProducts
join oi in CRM.tOrderItems on p.prodID equals oi.prodID
join o in CRM.tOrders on oi.orderID equals o.orderID
where o.status > 1 && p.active == true
group p by p.Name into nameGroup
orderby nameGroup.Count()
select new { Name = nameGroup.Key, Count = nameGroup.Count() };
See comment to question.
How to do "order by" in linq ->
If you have
var alist = .... select new { prd = p, ord = o };
you can do
alist.sort( (a, b) => a.ord.CompareTo(b.ord) );
to sort it in place.

Categories

Resources