I have following LINQ statement:
from o in Orders
join od in OrderDetails on o.OrderNumber equals od.OrderNumber
join r in RMAs on o.OrderNumber equals r.OrderNumber
join rd in RMADetails on r.RMAnumber equals rd.RMAnumber
from i in Inventory
where( a => ( od.SKU == a.LocalSKU)).DefaultIfEmpty()//error is here
where (r.Status != "Pending" && od.Adjustment == false)
select new
{
r.Status,
o.Name,
o.Company,
o.Address,
o.Address2,
o.City,
o.State,
o.Country,
o.Email,
o.Zip,
o.Phone,
o.ShipName,
o.ShipCompany,
o.ShipAddress,
o.ShipAddress2,
o.ShipCity,
o.ShipCountry,
o.ShipState,
o.ShipPhone,
o.ShipZip,
o.OrderNumber,
o.ShippingTotal,
OrderDate = o.OrderDate,
SerialNumbers = rd.SerialNumbers ?? "",
o.SourceOrderID
}
It's giving Invalid Where term. What I want to use LEFT OUTER JOIN having SQL Equivalent left join Inventory i on od.SKU = i.LocalSKU
Try this:
var qry = from o in Orders
join od in OrderDetails.Where(od=>od.Adjustment == false) on o.OrderNumber equals od.OrderNumber
join i in Inventory on i.LocalSKU equals od.SKU into grp
from g in grp.DefaultIfEmpty()
join r in RMAs.Where(r=>r != 'Pending') on o.OrderNumber equals r.OrderNumber
join rd in RMADetails on r.RMAnumber equals rd.RMAnumber
select new
{
//set of columns here
};
Related
I am trying to convert a SQL statement to LINQ.
I have this as SQL:
SELECT mutDetail.*
FROM hallo.Mutatie as mut
JOIN hallo.MutatieDetail AS mutDetail ON mut.MutatieID = mutDetail.fkMutatieID
JOIN tblMedewerker AS med ON med.MedewerkerID = mut.fkMedewerkerID
JOIN tblKlant AS klant ON klant.KlantID = med.fkKlantID
WHERE mutDetail.BronCode = 'Personeelsnummer'
AND klant.KlantNummer = '78555522'
And this is my LINQ statement:
public IQueryable<Mutatie> GetMutatiesInstroom()
{
var mutaties = (from m in context.Mutatie
join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
join klant in context.tblKlant on med.fkKlantID equals klant.KlantID
select m);
return mutaties;
}
But how to write the where statement in LINQ?
Thank you
I don't know what the problem was, but isn't it simple? Before select m:
where m.BronCode == "Personeelsnummer" && klant.KlantNummer == "8700"
Complete query as requested:
var mutaties = from m in context.Mutatie
join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
join klant in context.tblKlant on med.fkKlantID equals klant.KlantID
where m.BronCode == "Personeelsnummer" && klant.KlantNummer == "8700"
select m;
It should go here:
public IQueryable<Mutatie> GetMutatiesInstroom()
{
var mutaties = (from m in context.Mutatie
join mutDetail in context.MutatieDetail on m.MutatieID equals mutDetail.fkMutatieID
join med in context.tblMedewerker on m.fkMedewerkerID equals med.MedewerkerID
join klant in context.tblKlant on med.fkKlantID equals klant.KlantID
where (mutDetail.BronCode == "Personeelsnummer" &&
klant.KlantNummer == "8700")
select m);
return mutaties;
}
Here is an example from microsofts documentation on how to add a filter in Linq statement.
var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/basic-linq-query-operations
right under section Filtering
Please review my Linq Query and suggest the fix. THe second Join is giving error.
var result = (from e in _DB_Goal_Allocation.Employees.Where(x => x.Active == 1 && x.Branch_Number == ddlBranch)
join j in _DB_Goal_Allocation.Employees_JobTitleGroupLOBs.Where(y => Job_Group.Contains(y.JobGroup)) on e.Title equals j.JobTitle
join g in _DB_Goal_Allocation.Goal_Allocation_Employees.Where(ga => ga.Year == ddlYear)
on new{ e.Branch_Number, e.EmployeeID } equals new { Branch_Number = g.BranchNumber, EmployeeID = g.EmployeeID } into emp
from t in emp.DefaultIfEmpty()
select e.FirstName + " " + e.LastName as EmployeeName).ToList();
I am unable to execute the below linq.
var items( from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();
Here is the sql for the same for your reference.
select a.FirstName,b.MURDate,c.MURQuestionnaireId,c.MURWorksheetAnswersID from Patients as a
left join MURWorksheet as b on a.PatientId = b.PatientId
left join MURWorksheetAnswers as c on b.MURWorksheetId = c.MURWorksheetId
You are missing an equals sign in your pasted code:
var items = (from p in Patients
join q in MURWorksheets on p.PatientId equals q.PatientId into step1
from s in step1.DefaultIfEmpty()
join t in MURWorksheetAnswers on s.MURWorksheetId equals t.MURWorksheetId into step2
from s2 in step2.DefaultIfEmpty()
select new {p.FirstName , Date = (s.MURDate == null ? DateTime.Now.Date : s.MURDate),
s2.MURQuestionnaireId,s2.MURExpctedAnswersId}).ToList();
Problem: Am trying to rewrite this in Linq:
listOfUsersForReviewer is an IEnumerable<User>
int countOfGreenUsers = 0;
foreach (var user in listOfUsersForReviewer)
{
var u = (from reviewitems in context.ReviewItems
join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals groupaccountlinks.GroupAccountID
join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals reviewers.ReviewerID
join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
join users in context.RBSUsers on accounts.UserID equals users.UserID
where
users.UserID == user.UserID &&
reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
select reviewitems);
byte colour = (byte)Enums.UserStatusColour.Red;
if (u.Count() == 0)
{
colour = (byte)Enums.UserStatusColour.Green;
countOfGreenUsers++;
}
}
have tried to create an anonymous type, however this doesn't compile.
// select number of green users
var x = from user in listOfUsersForReviewer
from reviewitems in context.ReviewItems
join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals
groupaccountlinks.GroupAccountID
join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals
reviewers.ReviewerID
join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
join users in context.RBSUsers on accounts.UserID equals users.UserID
where
users.UserID == user.UserID &&
reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
select new
{
UserID = user.UserID,
CountOfGreen = reviewitems.Count()
};
Add grouping clause
var x = from user in listOfUsersForReviewer
from reviewitems in context.ReviewItems
join groupaccountlinks in context.GroupAccountLinks on reviewitems.GroupAccountID equals
groupaccountlinks.GroupAccountID
join reviews in context.Reviews on reviewitems.ReviewID equals reviews.ReviewID
join applications in context.Applications on reviews.ApplicationID equals applications.ApplicationID
join reviewers in context.Reviewers on applications.ResponsibleReviewerID equals
reviewers.ReviewerID
join accounts in context.Accounts on groupaccountlinks.AccountID equals accounts.AccountID
join users in context.RBSUsers on accounts.UserID equals users.UserID
where
users.UserID == user.UserID &&
reviewers.FullyQualifiedLogin == fullyQualifiedLogin &&
reviews.ReviewStatusID == (byte)Enums.ReviewStatus.InProgress &&
reviewitems.ReviewItemStatusID == (byte)Enums.ReviewItemStatus.Unapproved
group user by user.UserID into grouping
select new
{
UserID = grouping.Key,
CountOfGreen = grouping.Count()
};
I ended up rethinking me logic and split this query into 2 simpler ones.
Tools I used were: SQL Server Management Studio (graphical representation), Linqer and potentially Linqpad
And writing the query in English helped a lot.
For the below code it gives the error
type reference failed in the call to Join
how can to fix this
var unp = from v in context.student.Include("subjects").Include("marks")
join n in context.AllStudents
on v.StudentDetails.student_Id equals n.Id
join ss in context.StudentHistory
on v.StBacklogs_Id equals ss.Id
join userName in context.Users
on v.CreatedBy_Id equals userName.Id
join externalId in context.Books
on new { ss.BookNumber, ss.Id } equals new { externalId.BookNumber, externalId.Id }
select new { v, n, ss, userName,externalId };