my Query:
var s = entities.Doctors.SingleOrDefault(x => x.AreaId == id);
var z = (from x in entities.Doctors
join y in entities.Areas on x.AreaId equals y.AreaId
join s1 in entities.Availabilties on s.D_Id equals s1.DoctorId
join s2 in entities.Eductions on s.D_Id equals s2.DoctorId
join s3 in entities.DoctorSpecialities on s.D_Id equals s3.DoctorId
join s4 in entities.Specialities on s3.SpecialityId equals s4.SpecialityId
join s5 in entities.DaysDetails on s1.DaysId equals s5.DaysId
join s6 in entities.Degrees on s2.DegreeId equals s6.DegreeId
where x.AreaId.Equals(id)
select new DoctorDisplay
{
D_name = x.D_Name,
D_address = x.D_Address,
D_Area = y.AreaName,
D_Contact1 = x.D_Contactone,
D_Contact2 = x.D_Contacttwo,
D_fax = x.D_Faxno,
D_SpecialityName = s4.SpecialityName,
D_Availstarttime = s1.StartTime,
D_Availlasttime = s1.LastTime,
D_Availday= s5.DaysName,
D_DegreeName = s6.DegreeName,
D_Awards = x.D_Address,
D_Status = x.D_Status
}).ToList();
****Getting Output****
[{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"9PM","D_Availlasttime":"11PM","D_Availday":"Sunday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Dietician","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"MBBS","D_Awards":"block123","D_Status":"Available"},{"D_name":"Shanu","D_address":"block123","D_Area":"Manama","D_Contact1":"123","D_Contact2":"456","D_fax":"789","D_SpecialityName":"Ayurveda","D_Availstarttime":"4AM","D_Availlasttime":"11AM","D_Availday":"Monday","D_DegreeName":"Dentistdegree","D_Awards":"block123","D_Status":"Available"}]
Group_concat on the basis of Education Id, DoctorspecialitiesID, Avaibilities Id,
Image of Input and desire Output
Looking at you example output, it looks less like you want to "concat the data by ','" as much as you want to convert it to JSON.
Inwhichcase, you'd probably be better off using a tool like Newtonsoft's JSON.NET
UPDATE (based on comments):
Assume doctors is the result of the query given in question.
var output =
from d in doctors
group d by new { d.D_NAME, d.D_Area } into dg
select new DoctorDisplay
{
D_NAME = dg.Key.D_NAME,
D_Area = dg.Key.D_Area,
D_SpecialityName = String.Join(",", dg.Select(d => d.D_SpecialityName).Distinct()),
D_DegreeName = String.Join(",", dg.Select(d => d.D_DegreeName).Distinct())
};
I am getting duplication meals here
IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS
join ml in this.dbEntity.HOT_MEALS_PRICE on m.MEALSID equals ml.MEALSID into mls
from mls1 in mls.DefaultIfEmpty()
where mls1.HOTID==hotelId
select new DTOHotMealsPrice
{
MEALSID = m.MEALSID,
MEALSNAME = m.MEALSNAME,
CHPRICE = mls1.CHPRICE,
PRICE = mls1.PRICE,
HOTID = mls1.HOTID
}).Distinct().ToList();
I want to list all HOT_MEALS and also join with
HOT_MEALS_PRICE when a mealsid reference on it
When mls1.HOTID==hotelId, this will getting innerjoin results
How could it will a proper result
Two Way solve Solution
Remove Where Case
Add where Case In Join table
Show for Where Case Blow Linq Query
first Query
IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS
join ml in this.dbEntity.HOT_MEALS_PRICE.where(c=>c.HOTID==hotelId) on m.MEALSID equals ml.MEALSID into mls
from mls1 in mls.DefaultIfEmpty()
select new DTOHotMealsPrice
{
MEALSID = m.MEALSID,
MEALSNAME = m.MEALSNAME,
CHPRICE = mls1.CHPRICE,
PRICE = mls1.PRICE,
HOTID = mls1.HOTID
}).Distinct().ToList();
Second is:
IEnumerable<DTOHotMealsPrice> lst = (from m in this.dbEntity.HOT_MEALS
join ml in this.dbEntity.HOT_MEALS_PRICE on m.MEALSID equals ml.MEALSID into mls
from mls1 in mls.where(c=>c.HOTID==hotelId).DefaultIfEmpty()
select new DTOHotMealsPrice
{
MEALSID = m.MEALSID,
MEALSNAME = m.MEALSNAME,
CHPRICE = mls1.CHPRICE,
PRICE = mls1.PRICE,
HOTID = mls1.HOTID
}).Distinct().ToList();
I need to do a left join using LINQ.
I'm using this code:
var query = (from Geral in listEstadosGeral
join Coberto in listEstadoCoberto
on Geral.Id equals Coberto.Id
into temp
from j in temp.DefaultIfEmpty()
select new Estado()
{
Sigla = Geral.Sigla,
Nome = Geral.Nome
}).ToList();
In listEstadoGeral I have 27 records and in listEstadoCoberto I have 5 records.
The query has 27 records.
What is wrong with this code?
If you want to get the records that are present in listEstadosGeral that are not in listEstadoCoberto :
var result = listEstadosGeral.Where(p => !listEstadoCoberto.Any(p2 => p2.ID == p.ID)).ToList();
var query = (from Geral in listEstadosGeral
join Coberto in listEstadoCoberto
on Geral.Id equals Coberto.Id
select new Estado()
{
Sigla = Geral.Sigla,
Nome = Geral.Nome
}).ToList();
I have the following SQL that I would like to write as a single linq statement:
SELECT
P.PartyId,
P.PartyDate,
SUM(COALESCE(R.PaidAmount, 0)) AS AmountPaid
FROM
Party AS P
LEFT JOIN Reservation as R
ON P.PartyID = R.PartyID
GROUP BY P.PartyID, P.PartyDate
ORDER BY P.PartyDate DESC
The best I can do is use two sets of linq queries, like so:
var localList = from partyList in localDb.Parties
join reservationList in localDb.Reservations on
partyList.PartyID equals reservationList.PartyID into comboList
from newList in comboList.DefaultIfEmpty()
select new PartyAmounts {
PartyID = partyList.PartyID,
PartyDate = partyList.PartyDate,
AmountPaid = (newList.PaidAmount ?? 0) };
var secondList = from groupList in localList
group groupList by new {
groupList.PartyID,
groupList.PartyDate} into resList
select new PartyAmounts {
PartyID = resList.Key.PartyID,
PartyDate=resList.Key.PartyDate,
AmountPaid = resList.Sum(x => x.AmountPaid)};
I don't care if it's a method chain or a lambda but I would love to know how this is supposed to go together. I can only barely understand the two I've got now.
Thanks for the help.
var list = from partyList in localDb.Parties
join reservationList in localDb.Reservations on partyList.PartyID equals reservationList.PartyID into comboList
from details in comboList.DefaultIfEmpty() // Left join
group details by new {partyList.PartyID, partyList.PartyDate} into grouped // So that the group have both keys and all items in details
select new PartyAmounts
{
PartyID = grouped.Key.PartyID,
PartyDate = grouped.Key.PartyDate,
AmountPaid = grouped.Sum(x => x.AmountPaid ?? 0)}
};
I'm having trouble understanding a left outer join count in Linq to Entities.
My query is:
SELECT Locations.LocationId, Locations.LocationName, LocationPrizes.PrizeId, LocationPrizes.PrizeQuantity, Prizes.PrizeName, ISNULL(COUNT(WonPrizes.WonPrizeId), 0) AS WonPrizes
FROM Locations
INNER JOIN LocationPrizes ON Locations.LocationId = LocationPrizes.LocationId INNER JOIN Prizes ON LocationPrizes.PrizeId = Prizes.PrizeId
LEFT OUTER JOIN WonPrizes ON Locations.LocationId = WonPrizes.LocationId AND Prizes.PrizeId = WonPrizes.PrizeId
GROUP BY Locations.LocationId, Locations.LocationName, LocationPrizes.PrizeId, LocationPrizes.PrizeQuantity, Prizes.PrizeName
My Linq is:
var locationPrizes = from l in context.Locations
select new
{
l.LocationId,
l.LocationName,
Prizes = from o in l.LocationPrizes
select new
{
o.PrizeId,
o.PrizeQuantity,
o.Prize.PrizeJson
}
};
I can't get the count on teh left outer join part working correctly. Any pointers?
If you happen to have an association from LocationPrizes -> WonPrizes you can just do this:
var locationPrizes =
from l in context.Locations
select new
{
l.LocationId,
l.LocationName,
Prizes =
from o in l.LocationPrizes
select new
{
o.PrizeId,
o.PrizeQuantity,
o.Prize.PrizeJson
WonPrizes = o.WonPrizes.Count();
}
};
If not, this will work too (works for me with the minor edits below):
var locationPrizes =
from l in context.Locations
select new
{
l.LocationId,
l.LocationName,
Prizes =
from o in l.LocationPrizes
select new
{
o.PrizeId,
o.PrizeQuantity,
o.Prize.PrizeJson
WonPrizes =
(from w in context.WonPrizes
where w.PrizeId == o.PrizeId
&& w.LocationId == l.LocationId
select w)
.Count()
}
};