How to convert store procedure to linq in nopCommerce c# - c#

How to convert store procedure to linq in nopCommerce c#
My store procedure query
SELECT p.Id
FROM Product p WITH (NOLOCK)
LEFT JOIN Discount_AppliedToProducts dap WITH (NOLOCK)
ON p.Id = dap.Product_Id
LEFT JOIN Product_Category_Mapping pcm WITH (NOLOCK)
ON p.Id = pcm.ProductId
LEFT JOIN Discount_AppliedToCategories dac WITH (NOLOCK)
ON pcm.CategoryId = dac.Category_Id
AND dac.Category_Id IN (1 ,2 ,3 ,4 ,5 ,6)
LEFT JOIN Product_Manufacturer_Mapping pmm WITH (NOLOCK)
ON p.Id = pmm.ProductId
LEFT JOIN Discount_AppliedToManufacturers dam WITH (NOLOCK)
ON pmm.ManufacturerId = dam.Manufacturer_Id
WHERE dap.Discount_Id IN (3)
OR dac.Discount_Id IN (3)
OR dam.Discount_Id IN (3)
My linq query
var productlist = (from q in _productRepository.Table
select q).ToList();
var discount_AppliedToProductIds = (from dp in _discountRepository.Table
from p in dp.AppliedToProducts
select p).ToList().DistinctBy(d => d.Id).ToList();
var discount_AppliedToCategorieIds = (from dp in _discountRepository.Table
from c in dp.AppliedToCategories
select c).ToList().DistinctBy(d => d.Id).ToList();
var discount_AppliedToManufacturerIds = (from dp in _discountRepository.Table
from m in dp.AppliedToManufacturers
select m).ToList().DistinctBy(d => d.Id).ToList();
var product_Manufacturer_Mapping = (from dp in productlist
from pm in dp.ProductManufacturers
select pm).ToList().DistinctBy(d => d.Id).ToList();
var product_Category_Mapping = (from dp in productlist
from pc in dp.ProductCategories
select pc).ToList().DistinctBy(d => d.Id).ToList();
var ss = (from p in productlist
join dap in discount_AppliedToProductIds on p.Id equals dap.Id
join pcm in product_Category_Mapping on p.Id equals pcm.ProductId
//join dac in discount_AppliedToCategorieIds on pcm.CategoryId equals dac.Id
from dac in discount_AppliedToCategorieIds
join pmm in product_Manufacturer_Mapping on p.Id equals pmm.ProductId
join dam in discount_AppliedToManufacturerIds on pmm.ManufacturerId equals dam.Id
from dapd in dap.AppliedDiscounts
from pacd in dac.AppliedDiscounts
from damd in dam.AppliedDiscounts
where discountIds.Any(d => dapd.Id == d || d == pacd.Id || d == damd.Id)
// innner join condition
where categoryIds.Any(d => d == dac.Id) && dac.Id == pcm.CategoryId
select p).ToList();
I have write this code into c# but this code not provide proper result. Now I don't know what is problem into this code. If I run this code into sql server, then I get proper result, but in c# code I don't get proper result.

It's been a long time since I wrote a query in LINQ, but I seem to recall that if you wish to model a LEFT JOIN, you have to use DefaultIfEmpty(), otherwise you end up with an INNER JOIN.
See this, it's answer shows where to apply DefaultIfEmpty:
Linq join iquery, how to use defaultifempty
Obviously if you don't correctly model a LEFT JOIN expression, you'll end up with results only when all 3 inputs produce values.
I would also suggest not using .ToList() on each of your source queries, because that's going to manifest data into memory and use LINQ to Objects for your final query. If you remove the .ToList(), LINQ will construct a single database query for the entire process.
Mark

Related

The query has been canceled because the estimated cost of this query (27195) exceeds the configured threshold of 3000 in SQL Server 2014

I am using LINQ query to get rows with joining multiple tables and also used let keyword to get the list of rows for displaying multiple values in a display column. What's wrong with this linq query?
ImageLink
(from app in _dbContext.Appointments
join patient in _dbContext.Patients on app.PatientID equals patient.ID
join doc in _dbContext.Doctors on app.DoctorID equals doc.ID
join avail in _dbContext.DoctorsAvailabilities on app.AvailabilityID equals avail.ID
join loc in _dbContext.OfficeLocations on app.LocationID equals loc.ID
join state in _dbContext.CategoryDetails on loc.USState equals state.ItemID into StateGroup
from state in StateGroup.DefaultIfEmpty()
join appoint in _dbContext.CategoryDetails on app.Status equals appoint.ItemID into AppointmentStatusGroup
from appoint in AppointmentStatusGroup.DefaultIfEmpty()
let reasonVisit = (from v in _dbContext.DoctorAppointmentVisitReasons
join c in _dbContext.CategoryDetails on v.ReasonID equals c.ItemID
where c.CategoryID == CatergoryConstant.ReasonofVisit && v.AppointmentID == app.ID
select c.MasterData).ToList()
join appointmentSource in _dbContext.CategoryDetails on app.AppointmentSource equals appointmentSource.ItemID into AppointmentSourceGroup
from appointmentSource in AppointmentSourceGroup.DefaultIfEmpty()
let getReport = (from st in _dbContext.DoctorReportStatus
join cat in _dbContext.CategoryDetails on st.ReportStatus equals cat.ItemID
where st.StatusID == StatusConstant.Active && st.AppointmentID == app.ID
select new DoctorReportStatusViewModel() { ReportStatusText = cat.MasterData, ReportStatus = st.ReportStatus, AppointmentID = st.AppointmentID }).ToList()
let specility = (from s in _dbContext.DoctorSpecialities
join c in _dbContext.CategoryDetails on s.SpecialitiesID equals c.ItemID
where c.CategoryID == CatergoryConstant.DoctorSpeciality && s.DoctorID == doc.ID
select c.MasterData).ToList()

Linq join with newest record of second table

I have two related tables Ticket and Status.
Every ticket can be multiple status. (Open, assigned, closed). But I want all tickets but only one status (newest date) to show.
I could handle with this query on t-sql;
SELECT d.ticketID, statusName, c.statusDate, c.assignedTo,c.statusID
FROM Ticket d LEFT JOIN Status c ON c.ticketID = d.ticketID
WHERE c.statusID = (
SELECT MAX(statusID)
FROM Status c2
WHERE c2.ticketID = d.ticketID)
This is my Linq :
var result = from t in db.Ticket
join s in db.Status.OrderByDescending(x=>x.statusDate).Take(1)
on t.ticketID equals s.ticketID join c in db.Customer
on t.customerID equals c.customerID
But this only returning one row.
I solve my problem with linqer application via converting my Tsql query to linq
from s in db.Status
where s.statusID ==
(from c2 in db.Status where
c2.ticketID == s.Ticket.ticketID &&
c2.statusName == "New" ||
c2.statusName == "Assigned"
select new
{
c2.statusID
}).Max(p => p.statusID)
Thanks for everyone.
Try changing your original linq logic to:
var result = from t in db.Ticket
join s in db.Status on t.ticketID equals s.ticketID into sGroup
from s in sGroup.OrderByDescending(x=>x.statusDate).Take(1)
join c in db.Customer
on t.customerID equals c.customerID

Transform SQL query to Linq left join clause incorrect

I'm trying to select the MatchingObjects that doesn't exist in the Unlock table , I have this SQL query as following:
select a.* from MatchingObjects a
left join Unlocks b
on a.ObjectCategoryId = b.ObjectCategoryId
left join Members c
on b.StudentId = c.Id
and b.StudentId = #studentId
where b.ObjectCategoryId is null
and c.id is null
order by a.ObjectCategoryId
And a LINQ query
var query = (from d in db.ObjectCategories
join a in db.MatchingObjects on d.Id equals a.ObjectCategoryId into grp3
join b in db.Unlocks
on d.Id equals b.ObjectCategoryId into grp1
from m in grp1.DefaultIfEmpty()
join c in db.Members
on m.StudentId equals c.Id into grp2
from n in grp2.DefaultIfEmpty()
where m.ObjectCategoryId == null
&& n.Id == null
orderby d.Id).AsEnumerable()
;
However, the LINQ query is not showing the same result as that I want like in the SQL query. Could you guys tell me what I should change in my LINQ Query?
This is the model:
Better you can use below tools:
An SQL-> LINQ converter..
http://www.sqltolinq.com
http://www.linqpad.net/
try this one
var query = from m in db.MatchingObjects.Where(w => w.ObjectCategoryId == null)
join u in db.Unlocks.Where(w => w.studentId == #studentId)
on m.ObjectCategoryId equals u.ObjectCategoryId into joinedU
from ju in joinedU.DefaultIfEmpty()
join m in db.Members.Where(w => w.id == null)
on ju.StudentId equals m.Id into joinedM
from jm in joinedM.DefaultIfEmpty()
select m;
But your request is a bit Strange. You make the join by ObjectCategoryId and in the Where clause you put ObjectCategoryId == null!!!
sorry guys, the Sql and the LINQ query was a bit different in table selection. I'm sorry about that because I was trying different Linq while posting the question, but The main problem is at the join clause
(from d in db.ObjectCategories
join a in db.MatchingObjects
on d.Id equals a.ObjectCategoryId into grp3
join b in db.Unlocks
on d.Id equals b.ObjectCategoryId into grp1
from m in grp1.DefaultIfEmpty()
join c in db.Members
on m.StudentId equals studentId into grp2
from n in grp2.DefaultIfEmpty()
where m.ObjectCategoryId == null
where n.Id == null
orderby d.Id).AsEnumerable()
/* this is the correct one */
join c in db.Members
on m.StudentId equals studentId into grp2
/* the below was the original incorrect join clause*/
join c in db.Members
on m.StudentId equals c.Id into grp2

Making 1 table join on 2 other tables with LINQ

I'm trying to join 1 table onto 2 other tables with LINQ, but I can't seem to figure out how this is done.
I can make it work writing pure SQL statements within Visual studio, I'm just not sure how to convert this into LINQ.
Here's my SQL statement:
SELECT c.CustomerId, c.CustomerName, pw.Number, pc.Number FROM Customers as c
LEFT JOIN Tasks as k ON k.Id = c.Task_Id
LEFT JOIN Workers as w ON w.Id = k.Worker_Id
LEFT JOIN PersonNumbers as pw ON pw.Person_Id = w.Id
LEFT JOIN Chiefs as ch ON ch.Id = k.Chief_Id
LEFT JOIN PersonNumbers as pc ON pc.Person_Id = ch.Id
Perhaps this requires a bit of explanation.
We have a bunch of Customers and these customers can have some tasks. Within a task you will have workers and chiefs. Within the PersonNumbers table, I have some extra information about workers and chiefs, and this is the information that I need.
You should be able to do something like the following assuming all your joins are based on foreign keys that should result in navigation properties in your entities. The DefaultIfEmpty is what makes everything a left join.
var results = from c in db.Customers
from k in c.Tasks.DefaultIfEmpty()
from w in k.Workers.DefaultIfEmpty()
from pw in w.Persons.DefaultIfEmpty()
from ch in k.Chiefs.DefaultIfEmpty()
from pc in ch.Persons.DefaultIfEmpty()
select new
{
c.CustomerId,
c.CustomerName,
pw.Number,
pc.Number
};
If you don't have the navigation properties then you'll have to use join.
var results = from c in db.Customers
join xk in db.Tasks on xk.Id equals c.Task_Id
from k in xk.DefaultIfEmpty()
join xw in db.Workers on xw.Id equals k.Worker_Id
from w in xw.DefaultIfEmpty()
join xpw in db.Persons on xpw.Person_Id equals w.Id
from pw in xpw.DefaultIfEmpty()
join xch in db.Chiefs on xch.Id equals k.Chief_Id
from ch in xch.DefaultIfEmpty()
join xpc in db.Persons on xpc.Person_Id euals ch.Id
from pc in xpc.DefaultIfEmpty()
select new
{
c.CustomerId,
c.CustomerName,
pw.Number,
pc.Number
};

Can't convert T-SQL INNER JOIN to LINQ-Entities query

T-SQL:
declare #postlocations table (locationid int)
insert into #postlocations
select locationid
from dbo.PostLocations
where PostId = 162172
select t.*
from dbo.Themes t
inner join dbo.ThemeLocations tl on t.ThemeId = tl.ThemeId
inner join #postlocations pl on tl.LocationId = pl.locationid
LINQ-Entities i have so far:
var postLocations = e.SomePost.Locations; // pre-fetched, e.g materialized ICollection<Post>
var themes = (from t in db.Themes
join q in postLocations on t.Locations.Select(l => l.LocationId) equals q.LocationId
select t).ToList();
But the compiler is complaining on the join keyword about not being able to infer the type arguments.
Any ideas?
I don't think you can join a SQL table with an in-memory list of objects, even if those objects are originally from the database.
Convert the in-memory list of objects to a list of id's (integer), and use that in the join or in a Contains/sub-select. EF can translate the list of id's to parameters when generating the SQL.
The problem with your join is that you're implying a collection of LocationId (t.Locations.Select(l => l.LocationId) can equal a single LocationId. You're trying to join a Theme which has a collection of Locations onto a single Location.
You should be able to fix this by using Contains
var themes = (from t in db.Themes
join q in postLocations
on t.Locations.Select(l => l.LocationId).Contains(q.LocationId)
select t).ToList();
or if EF complains about passing a postLocations as a parameter, you can try
// I'd materialize this but you may not have to
var postLocationIds = postLocations.Select(p => p.LocationId).ToList();
var themes = db.Themes.Where(t => t.Locations.Any(l =>
postLocationIds.Contains(l.LocationId))).ToList();
Edit
how about this
///your sql query
select t.* from dbo.Themes t
inner join dbo.ThemeLocations tl on t.ThemeId = tl.ThemeId
inner join #postlocations pl on tl.LocationId = pl.locationid
//linq query for that
from t in teams
join from tl in teamlocation on t.themid = tl.ThemeID
join from pl in postlocation on tl.temeid = pl.temeid
select t;
Org
Not sure but you can try out by using let keyword
var themes = (from t in db.Themes
let location = t.Locations
join q in postLocations on location.LocationId equals q.LocationId
select t).ToList();

Categories

Resources