I'm trying to rewrite sql query to linq but can't do it myself.
The most problem for me is to get I,II and III aggregated values.
Sql query:
select o.Name,t.TypeID, SUM(e.I),SUM(e.II),SUM(e.III) from Expenditure e
join Finance f on f.FinanceId = e.FinanceId
join FinanceYear fy on fy.FinanceYearId = f.FinanceYearId and fy.StatusId = 1
join Project p on p.ProjectId = fy.ProjectId
join Organization o on o.OrganizationId = p.OrganizationId
join Type t on t.TypeID = p.TypeID
where fy.Year = 2018
group by o.Name,s.TypeID
and what I have done so far is:
var x = (from e in _db.Expenditures
join f in _db.Finances on e.FinanceId equals f.FinanceId
join fy in _db.FinanceYears on f.FinanceYearId equals fy.FinanceYearId and fy.StatusId = 1 // this does not work, cant join on multiple conditions?
join p in _db.Projects on fy.ProjectId equals p.ProjectId
join o in _db.Organizations on p.OrganizationId equals o.OrganizationId
join s in _db.Types on p.TypeId equals s.TypeId
group new { o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = ?,
II = ?,
III = ?,
}
);
Try something like this:
group new { e, o, s } by new { o.OrganizationId, s.TypeId }
into grp
select new AggModel
{
OrganizationId = grp.Key.OrganizationId,
TypeId = grp.Key.TypeId,
I = grp.Sum(a => a.e.I),
II = grp.Sum(a => a.e.II),
III = grp.Sum(a => a.e.III),
}
You'll need to adjust the right side of the lambda to navigate to the correct property.
You Need to use the Group by for aggregation methods.
Check the below link for more Knowledge.
How to use aggregate functions in linq with joins?
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 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)}
};
public static IEnumerable<AppCache> GetTopRatedApps(string language,bool isinitialized)
{
List<AppCache> objApps = new List<AppCache>();
objApps = GetAllApps(isinitialized,language).ToList();
List<RatingCache> objRatings = new List<RatingCache>();
objRatings = GetAllRatings();
var query =
from Apps in objApps
join ratings in objRatings
on Apps.AppId equals ratings.AppId where ratings.RatingGiven == 1
select new AppCache();
return query;
}
Stored Procedure:
select o.AppId, count(*) as ItemCount
from App o
inner join Rating od
on o.AppId = od.AppId
where od.RatingGiven = 1
group by o.AppId
Can't figure out how to get the item count from the list.
Not: AppCache is equivalent to App
This should be the translation of your stored procedure. If you want to return something else, just modify the select method.
var query = from Apps in objApps
join ratings in objRatings
on Apps.AppId equals ratings.AppId
where ratings.RatingGiven == 1
group Apps by Apps.AppId into g
select new { AppId = g.AppId, ItemCount = g.Count() }