简体   繁体   中英

Getting Total Record by Group by in Linq

I have one query in SQL, want to convert it to Linq. My Query is :

select count(tbs.tbsid) as CNTSeats,tbm.BusNo
from tdp_tourpackageschedule tps
join tdp_tourpackage tp on tp.TourID = tps.FK_TourID
join tdp_tourbusseats tbs on tbs.tbsid = tps.fk_tbsid
join tdp_tourbusmaster tbm on tbm.tbid = tbs.fk_tbid
where fk_tdid = @FKTDID and fk_TourID = @FKTourID and IsOpen = 1
group by tbm.BusNo

I tried this Code:

var tourAvail = (from ts in entities.tdp_TourPackageSchedule
                 join tp in entities.tdp_TourPackage on ts.FK_TourID equals tp.TourID
                 join tbs in entities.tdp_TourBusSeats on ts.FK_TBSID equals tbs.TBSID
                 join tb in entities.tdp_TourBusMaster on tbs.FK_TBID equals tb.TBID
                 where ts.FK_TDID == TDID && ts.FK_TourID == TourID && ts.IsOpen == 1
                 group tb by tb.BusNo into cnt
                 select new
                 {
                     //BusNo = cnt.bu
                     //Count = cnt.Select(x => x.tdp_TourBusSeats).Distinct().Count()
                 });

I don't know how to get count of records, anyone can help ?

Do :

var tourAvail = (from ts in entities.tdp_TourPackageSchedule
                 join tp in entities.tdp_TourPackage on ts.FK_TourID equals tp.TourID
                 join tbs in entities.tdp_TourBusSeats on ts.FK_TBSID equals tbs.TBSID
                 join tb in entities.tdp_TourBusMaster on tbs.FK_TBID equals tb.TBID
                 where ts.FK_TDID == TDID && ts.FK_TourID == TourID && ts.IsOpen == 1
                 group tb by tb.BusNo into cnt
                 select new
                 {
                     BusNo = cnt.Key,
                     Count = cnt.Count()
                 }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM