I have this so far:
var query = (from g in this.Context.Groups
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Then the SQL query I want to add is this:
select zipCode
from ADDRESSES
where ADDRESS_K in
(select ADDRESS_K
from GROUPADDRESS
where GROUP_K = "is in Group_K found above "
and address_type = "address type variable I pass into this method"
and ACTIVE = 1)
Notice that GROUPADDRESS is the bridge table between GROUPS and Addresses table that has Group_K and Address_K
I can't figure it out how to add a new LINQ query or update the one I have to account for the new SQL I am trying to add. Thanks for help.
Groups Table: Group_K, ID, TaxId
Addresses Table: Address_K, Zip
GroupAddress Table: Group_K, Address_K, Address_Type
Assuming you have a GroupAddress in your context:
var query = (from g in this.Context.Groups
join ga in this.Context.GroupAddress on g.AddressK equals ga.AddressK
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Edit:
Right so GroupAddress sits between Group and Address. Assuming you've set up relationships in your database, you will have navigation properties you can test against:
where g.Address == ''
EF drops link tables so your navigation is simpler. If not available in EF, add another join to the above.
var query = (from g in this.Context.Groups
join ga in this.Context.GroupAddress on g.Group_K equals ga.Group_K
join a in this.Context.GroupAddress on g.Address_K equals ga.Address_K
where g.ID == groupID &&
g.TaxId == groupTaxId
select g.Group_K);
Maybe something like this:
var groupID=1;
var groupTaxId=1;
var result=
(
from a in this.Context.ADDRESSES
where
(
from ga in this.Context.GROUPADDRESS
join g in this.Context.Groups
on g.GROUP_K equals ga.GROUP_K
where ga.ID == groupID
where g.TaxId== groupTaxId
where ga.ACTIVE == 1
select ga.ADDRESS_K
).Contains(a.ADDRESS_K)
select new
{
a.zipCode
}
).ToList();
Related
I have the following query I want to replicate in LINQ:
SELECT A.id,C.quotas, C.fundingid, C.date
,(SELECT value FROM public.fundingsharevalue WHERE fundingid = C.fundingid AND date = C.date) as sharevalue
FROM public.goal A
INNER JOIN public.goaltransactionfunding C ON A.id = C.goalid
where A.userid = 25
Code in LINQ:
var balance = from goal in _context.Goals
join goaltransactionfunding in _context.Goaltransactionfundings on goal.Id equals goaltransactionfunding.Goalid
join user in _context.Users on goal.Userid equals user.Id
where goal.Userid == userid
select new BalanceViewModel
{
FundingId = goaltransactionfunding.Fundingid,
ShareValue = (from fundingsharevalue in _context.Fundingsharevalues
where fundingsharevalue.Id == goaltransactionfunding.Fundingid && fundingsharevalue.Date.Equals(goaltransactionfunding.Date)
select fundingsharevalue.Value).FirstOrDefault(),
Quotas = (double)goaltransactionfunding.Quotas,
Fecha = goaltransactionfunding.Date,
GoalId = goal.Id
};
The problem is with Sharevalue subquery, it doesn't show values when actually it has.
It seems that the query does not fetch anything when it should fetch as it does in SQL.
I'm attaching images for both outputs.
QueryShareValues Image
LINQ sharevalues Image
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()
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
This is my SQL command
SELECT KEY,NAME
from COMPANY c
WHERE KEY IN (select KEY from USER_COMPANY where UserId = #UserId)
order by NAME asc
So I want to convert it to Entity Framework.
I try like this
var userCompany = (from u in db.USER_COMPANY
where u.UserId == UserId
select(u.KEY));
var user = (from c in db.COMPANY
where (c => userCompany.Contains(c.KEY)
select c);
but it is not working.
How to use the SQL IN keyword in Entity Framework?
Try this:
var query = from c in db.COMPANY
where (from u in db.USER_COMPANY
where u.UserId == UserId
select u.KEY).Contains(c.KEY)
orderby c.NAME
select c.KEY, c.NAME;
Note that this SQL query has the exact same meaning:
SELECT c.KEY, c.NAME
FROM COMPANY c
JOIN (SELECT DISTINCT KEY FROM USER_COMPANY where UserId = #UserId) u
ON U.KEY = C.KEY
ORDER BY c.NAME asc
So you should be able to just do:
var userCompany = (from u in db.USER_COMPANY
where u.UserId == UserId
select(u.KEY)).Distinct();
var result = from c in db.COMPANY
join u in userCompany
on c.KEY = u.KEY
select new {c.KEY, c.NAME};
My understanding is that the translation from .Contains() to IN is only just being added to EF6.
According to this work item (http://entityframework.codeplex.com/workitem/245) and the release note: Improved performance of Enumerable.Contains in LINQ queries.
So look for it in EF6
I am fairly new to Linq To SQL but trying to run what should be a fairly simple SQL query and can't figure out how to make it play nice in LINQ.
SELECT Users.Id, Users.Id AS Expr1, Users.FirstName, Users.LastName,
User_x_Territory.UserID
FROM Users LEFT OUTER JOIN
User_x_Territory ON User_x_Territory.UserID = Users.Id
GROUP BY Users.Id, Users.Id, Users.FirstName, Users.LastName, User_x_Territory.UserID
HAVING (COUNT(User_x_Territory.UserID) = 0)
Just trying to get all users that do not have a territory assigned, the only way to tell if they have a territory is to check the user_x_territory gerrund.
I am able to get all of the users out of my DB with this:
var users = from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select users;
But from there I can't figure out how to add a group by/having to refine the search results to only show users with no territories.
Thanks for any help.
I suggest the following solution.
db.Users.Where(u => u.User_x_Territories.Count == 0)
edit: heh, guess someone beat me to it :(
from t in db.Users
join t0 in db.User_x_Territory on new { UserID = t.Id } equals new { UserID = t0.UserID } into t0_join
from t0 in t0_join.DefaultIfEmpty()
group new {t, t0} by new {
t.Id,
Column1 = t.Id,
t.FirstName,
t.LastName,
t0.UserID
} into g
where g.Count() == 0
select new {
Id = g.Key.Id,
Expr1 = g.Key.Id,
g.Key.FirstName,
g.Key.LastName,
UserID = g.Key.UserID
}
I don't know how efficient this is (I'm guessing not very), but you could try something like this:
var users = (from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select u).Where(u => u.User_x_Territories.Count == 0);