Below is the LINQ Query:
var data1 = (from p in DB.TProject
from c in DB.TClient
join ct in DB.TContract
on c.ClientId equals ct.ClientId
join cp in DB.TClientPortfolio
on ct.ContractId equals cp.ContractId
select new ClientReport
{
ClientName = c.ClientName,
StartDate = ct.StartDate.ToString(),
EndDate = ct.EndDate.ToString(),
DeliveryTime1 = Convert.ToString(cp.DeliveryTime1)
}).Distinct().ToList();
here I need DeliveryTime in string where Delivery Time is in Timespan so want to convert it to string.
But is is showing error. Kindly help me
Thank you
Probably LINQ provider don't know how to convert TimeSpan to string. So do that after returning data from the server.
var query =
from p in DB.TProject
from c in DB.TClient
join ct in DB.TContract on c.ClientId equals ct.ClientId
join cp in DB.TClientPortfolio on ct.ContractId equals cp.ContractId
select new
{
ClientName = c.ClientName,
StartDate = ct.StartDate,
EndDate = ct.EndDate,
DeliveryTime1 = cp.DeliveryTime1
};
var data1 = query
.Distinct()
.AsEnumerable()
.Select(c => new ClientReport
{
ClientName = c.ClientName,
StartDate = c.StartDate.ToString(),
EndDate = c.EndDate.ToString(),
DeliveryTime1 = Convert.ToString(c.DeliveryTime1)
})
.ToList();
Related
I have this:
var lstAssignmentDetails =
(from t1 in _unitOfWork.ReferralDetailsRepository.Get()
.Where(m => (m.ChartStatusID == (int)Utility.ReferralChartStatus.NotStaffed
|| m.ChartStatusID == (int)Utility.ReferralChartStatus.Partially_Staffed
|| m.ChartStatusID == (int)Utility.ReferralChartStatus.Restaff)
&& m.IsDeleted == false).OrderByDescending(x => x.ReferralDetailsId)
join t2 in _unitOfWork.ReferralRepository.Get() on t1.ReferralId equals t2.ReferralId
join t3 in _unitOfWork.PatientRepository.Get() on t2.PatientId equals t3.PatientId
join t4 in _unitOfWork.ClientRepository.Get() on t2.ClientId equals t4.ClientID
join t5 in _unitOfWork.DisciplineRepository.Get() on t1.DesciplineID equals t5.DesciplineID
join t6 in _unitOfWork.UserRepository.Get() on t2.CreatedBy equals t6.UserID
join t7 in _unitOfWork.PersonRepository.Get() on t6.PersonID equals t7.PersonID
join rt in _unitOfWork.ReferralTherapistRepository.Get() on t2.ReferralId equals rt.ReferralId
join t in _unitOfWork.TherapistRepository.Get() on rt.TherapistId equals t.TherapistId
join u in _unitOfWork.UserRepository.Get() on t.UserId equals u.UserID
join p in _unitOfWork.PersonRepository.Get() on u.PersonID equals p.PersonID
select new ReferralTempModel()
{
ReferralId = t1.ReferralId,
ClientName = t4.ClientName,
PatientName = t3.LastName + "," + t3.FirstName,
RefferalDate = t2.RefferalDate,
DisciplineID = t1.DisciplineID,
ReferralDetailsId = t1.ReferralDetailsId,
PatientId = t2.PatientId,
ClientID = t4.ClientID,
DiscName = t5.DesciplineType,
IsRejected = t1.IsRejected,
CreatedBy = t7.LastName + "," + t7.FirstName,
ChartstatusId = t1.ChartStatusID.Value
}).ToList();
return lstAssignmentDetails;
There are multiple ReferralTherapists for each referral. I need all the ReferralTherapists for each referral to be concatenated into one field. Essentially what I want to do is a GROUP_CONCAT using LINQ.
I know this is probably a duplicate, but none of the previous questions handles anything as complicated as this. Trying to add GroupBy always seems to create an error no matter where I place it.
Start by not joining other referrals as t2 when constructing your flat list, and adding therapist's name to the anonymous type:
var flatListAssignmentDetails =
(from t1 in _unitOfWork.ReferralDetailsRepository.Get()
.Where(m => (m.ChartStatusID == (int)Utility.ReferralChartStatus.NotStaffed
|| m.ChartStatusID == (int)Utility.ReferralChartStatus.Partially_Staffed
|| m.ChartStatusID == (int)Utility.ReferralChartStatus.Restaff)
&& !m.IsDeleted)
join t2 in _unitOfWork.ReferralRepository.Get() on t1.ReferralId equals t2.ReferralId
join t3 in _unitOfWork.PatientRepository.Get() on t2.PatientId equals t3.PatientId
join t4 in _unitOfWork.ClientRepository.Get() on t2.ClientId equals t4.ClientID
join t5 in _unitOfWork.DisciplineRepository.Get() on t1.DesciplineID equals t5.DesciplineID
join t6 in _unitOfWork.UserRepository.Get() on t2.CreatedBy equals t6.UserID
join t7 in _unitOfWork.PersonRepository.Get() on t6.PersonID equals t7.PersonID
select new {
t1.ReferralId,
t4.ClientName,
PatientName = t3.LastName + "," + t3.FirstName,
t2.RefferalDate,
t1.DisciplineID,
t1.ReferralDetailsId,
t2.PatientId,
t4.ClientID,
DiscName = t5.DesciplineType,
t1.IsRejected,
CreatedBy = t7.LastName + "," + t7.FirstName,
ChartstatusId = t1.ChartStatusID.Value,
TherapistName = p.LastName + "," + p.FirstName
}).ToList();
Now you can group your list, and concatenate therapist names:
var listAssignmentDetails = flatListAssignmentDetails
.GroupBy(r => r.ReferralId)
.Select(g => new ReferralTempModel {
ReferralId = g.First().ReferralId,
ClientName = g.First().ClientName,
PatientName = g.First().PatientName,
RefferalDate = g.First().RefferalDate,
DisciplineID = g.First().DisciplineID,
ReferralDetailsId = g.First().ReferralDetailsId,
PatientId = g.First().PatientId,
ClientID = g.First().ClientID,
DiscName = g.First().DiscName,
IsRejected = g.First()..IsRejected,
CreatedBy = g.First().CreatedBy,
ChartstatusId = g.First().ChartStatusID,
TherapistNames = string.Join(", ", g.Select(r => r.TherapistName))
}).ToList();
One thing to note here is that all fields that are tied to all referrals in the group are obtained through g.First() construct. The field with "group concatenation" is produced using string.Join method on a projection of TherapistName from the group.
I have the following tables:
Users:
userId, userFirstName, userLastName.
holdBilling:
bEntityID, CarrierOID, PayerOID, holdTYPE, createUserID.
carrier:
carrierOID, carrierName.
payer:
payerOID, payerName.
I want the code to save in a new entity
holdBilling => new
{
FirstName, LastName, CarrierName, PayerName
}
One of these Entities has either a payer or a carrier value (cannot have both). Basically i want to make 2 left joins on the same table with one call. This would be the SQL query that would work for me.
SELECT TOP 1000 [ID]
,[bEntityID]
,c.carrierID
,c.carrierName
,p.payerID
,p.payerName
,[holdType] ( this is "C" for carrier and "P" for payer )
FROM .[dbo].[holdBilling] hb
left join dbo.payer p on hb.payerID = p.payerID
left join dbo.carrier c on hb.carrierID = c.carrierID
where [bEntityID] = 378
The temporary solution I've found is getting a list of all Carriers
var listC = (from hold in holdBilling
join u in Users on hold.createUserID equals u.userID
join c in carrier.DefaultIfEmpty() on hold.carrierID equals c.carrierID
select new
{
Elem = hold,
FName = u.userFirstName,
LName = u.userLastName,
Carrier = c.carrierName,
Payer = ""
}).ToList();
and one for all payers
select new
{
Elem = hold,
FName = u.userFirstName,
LName = u.userLastName,
Carrier = "",
Payer = p.payerName
}).ToList();
and merging the two,I'm confident there has to be a solution for doing both in one query.
Something like this maybe:
var listC = (
from hb in holdBilling
from p in payer.Where(a=>a.payerID==hb.payerID).DefaultIfEmpty()
from c in carrier.Where(a=>a.carrierID=hb.carrierID).DefaultIfEmpty()
where hb.bEntityID==378
select new
{
hb.bEntityID,
c.carrierID,
c.carrierName,
p.payerID,
p.payerName,
holdType=(payer==null?"P":"C")
}
).Take(1000)
.ToList();
You need to use DefaultIfEmpty to do a left join
var listC = (from hold in holdBilling
from u in Users.Where(x => hold.createUserID == x.userID).DefaultIfEmpty()
from c in carrier.Where(x => hold.carrierID == x.carrierID).DefaultIfEmpty()
select new
{
Elem = hold,
FName = u.userFirstName,
LName = u.userLastName,
Carrier = c.carrierName,
Payer = ""
}).ToList();
I am trying to do a relatively straight forward SQL query with linq:
SELECT ir.resource_id, r.first_name, r.surname, rt.job_title, SUM(plan_ts_hours), SUM(ts_hours) FROM tbl_initiative_resource ir
INNER JOIN tbl_resource r ON ir.resource_id = r.resource_id
INNER JOIN tbl_resource_type rt ON rt.resource_type_id = r.resource_type_id
LEFT JOIN tbl_plan_timesheet pts on pts.resource_id = ir.resource_id AND pts.initiative_id = ir.initiative_id
LEFT JOIN tbl_timesheet ts on ts.resource_id = ir.resource_id AND ts.initiative_id = ir.initiative_id
WHERE ir.initiative_id = 111
GROUP BY ir.resource_id, r.first_name, r.surname, rt.job_title
After reading this blog: http://smehrozalam.wordpress.com/2010/04/06/linq-how-to-build-complex-queries-utilizing-deferred-execution-and-anonymous-types/
I came up with the following linq:
var query = (from initRes in Context.tbl_initiative_resource
join res in Context.tbl_resource on initRes.resource_id equals res.resource_id
join resType in Context.tbl_resource_type on res.resource_type_id equals resType.resource_type_id
from tsheet in Context.tbl_timesheet.Where(x => x.resource_id == initRes.resource_id).Where(x => x.initiative_id == initRes.initiative_id).DefaultIfEmpty()
from plannedtsheet in Context.tbl_plan_timesheet.Where(x => x.resource_id == initRes.resource_id).Where(x => x.initiative_id == initRes.initiative_id).DefaultIfEmpty()
where initRes.initiative_id == initiativeID
group new { ID = res.resource_id, ResourceType = resType.job_title, Times = tsheet, P = plannedtsheet } by initRes.resource_id into g
select new
{
ResourceID = g.Key,
Hours = g.Sum(x => (decimal?)x.Times.ts_hours),
PlannedHours = g.Sum(x => (decimal?)x.P.plan_ts_hours)
}).ToList();
Any ideas on how I can access the ResourceType when selecting the new anonymous type?
ResourceType is part of the the grouping key, so g.Key.ResourceType should do it.
(Check out the type of ResouceID in the results, as you've assigned it g.Key it will be an instance of the (anonymous) type created in the group clause.
I am new to linq and want to perform joins across three tables. The sql equivalent of what I am trying to do is:
SELECT PM.Id, PM.new_clientId, .....
C.new_firstname, c.new_surname,.....
A.new_addressid, A.new_addressline1, A.new_addressline2.....
CY.new_county
FROM partialMatch PM
INNER JOIN new_client C ON PM.new_clientId = C.new_clientId
LEFT OUTER JOIN new_address A ON C.new_addressId = A.new_addressId
LEFT OUTER JOIN new_county CY ON A.new_countyId = CY.new_countyId
WHERE PM.ownerId = #UserId AND PM.new_reviewed <> true
The linq code I have developed is this, but these don't seem to be outer join as it is not returning any results unless I comment out the joins to the address and county table
var partialMatches = from pm in ContextService.CreateQuery<new_partialmatch>()
join c in ContextService.CreateQuery<new_client>() on pm.new_ClientId.Id equals c.new_clientId
join a in ContextService.CreateQuery<new_address>() on c.new_AddressID.Id equals a.new_addressId
join cy in ContextService.CreateQuery<new_county>() on a.new_CountyID.Id equals cy.new_countyId
where pm.OwnerId.Id == _currentUserId && pm.new_Reviewed != true
select new
{
Id = pm.Id,
new_ClientID = pm.new_ClientId,
new_MatchingCRMClientID = pm.new_MatchingCRMClientId,
new_MatchingVulcanClientID = pm.new_MatchingVulcanClientID,
new_name = pm.new_name,
firstname = c.new_FirstName,
surname = c.new_Surname,
dob = c.new_DateOfBirth,
addressId = a.new_addressId,
address1 = a.new_AddressLine1,
address2 = a.new_AddressLine2,
address3 = a.new_AddressLine3,
address4 = a.new_AddressLine4,
county = cy.new_County
};
Any help would be greatly appreciated,
Thanks,
Neil
EDIT:
I also tried using the 'into' statement but then on my second join the alias isn't recognised.
from pm in ContextService.CreateQuery<new_partialmatch>()
join c in ContextService.CreateQuery<new_client>() on pm.new_ClientId.Id equals c.new_clientId into pmc
from x in pmc.DefaultIfEmpty()
join a in ContextService.CreateQuery<new_address>() on c.new_AddressID.Id equals a.new_addressId
So for the c.newIddressID.Id equals a.new_addressId I get this error message:
The name 'c' is not in scope on the left side of 'equals'. Consider swapping the expressions on either side of 'equals'.
var addressCountryQuery = from a in ContextService.CreateQuery<new_address>()
from cy in ContextService.CreateQuery<new_county>().Where( cy => cy.new_countyId == a.new_CountyID.Id).DefaultIfEmpty()
select new
{
addressId = a.new_addressId,
address1 = a.new_AddressLine1,
address2 = a.new_AddressLine2,
address3 = a.new_AddressLine3,
address4 = a.new_AddressLine4,
county = cy.new_County
}
var partialMatches = from pm in ContextService.CreateQuery<new_partialmatch>()
join c in ContextService.CreateQuery<new_client>() on pm.new_ClientId.Id equals c.new_clientId
from a in addressCountryQuery .Where( a => a.addressId == c.new_AddressID.Id).DefaultIfEmpty()
where pm.OwnerId.Id == _currentUserId && pm.new_Reviewed != true
select new
{
Id = pm.Id,
new_ClientID = pm.new_ClientId,
new_MatchingCRMClientID = pm.new_MatchingCRMClientId,
new_MatchingVulcanClientID = pm.new_MatchingVulcanClientID,
new_name = pm.new_name,
firstname = c.new_FirstName,
surname = c.new_Surname,
dob = c.new_DateOfBirth,
addressId = a.addressId,
address1 = a.AddressLine1,
address2 = a.AddressLine2,
address3 = a.AddressLine3,
address4 = a.AddressLine4,
county = a.County
};
See how I modified the join for a and cy so I could add the DefultIfEmpty method.
I split the two outer joins in to one query and then join that query back into the original query.
I have a linq statement which calls a stored proc and returns a list of items and descriptions.
Like so;
var q = from i in doh.usp_Report_PLC()
where i.QTYGood == 0
orderby i.PartNumber
select new Parts() { PartNumber = i.PartNumber, Description = i.Descritpion.TrimEnd() };
I then have another SQL statement which returns the quantities on order and delivery date for each of those items. The Parts class has two other properties to store these. How do I update the existing Parts list with the other two values so that there is one Parts list with all four values?
UPDATE
The following code now brings out results.
var a = from a1 in db.usp_Optos_DaysOnHand_Report_PLC()
where a1.QTYGood == 0
orderby a1.PartNumber
select new Parts() { PartNumber = a1.PartNumber, Description = a1.Descritpion.TrimEnd() };
var b = from b1 in db.POP10110s
join b2 in db.IV00101s on b1.ITEMNMBR equals b2.ITEMNMBR
//from b3 in j1.DefaultIfEmpty()
where b1.POLNESTA == 2 && b1.QTYCANCE == 0
group b1 by new { itemNumber = b2.ITMGEDSC } into g
select new Parts() { PartNumber = g.Key.itemNumber.TrimEnd(), QtyOnOrder = g.Sum(x => Convert.ToInt32(x.QTYORDER)), DeliveryDue = g.Max(x => x.REQDATE).ToShortDateString() };
var joinedList = a.Join(b,
usp => usp.PartNumber,
oss => oss.PartNumber,
(usp, oss) =>
new Parts
{
PartNumber = usp.PartNumber,
Description = usp.Description,
QtyOnOrder = oss.QtyOnOrder,
DeliveryDue = oss.DeliveryDue
});
return joinedList.ToList();
Assuming your "other SQL statement" returns PartNumber, Quantity and DeliveryDate, you can join the lists into one:
var joinedList = q.Join(OtherSQLStatement(),
usp => usp.PartNumber,
oss => oss.PartNumber,
(usp, oss) =>
new Parts
{
PartNumber = usp.PartNumber,
Description = usp.Description,
Quantity = oss.Quantity,
DeliveryDate = oss.DeliveryDate
}).ToList();
You can actually combine the queries and do this in one join and projection:
var joinedList = doh.usp_Report_PLC().
Where(i => i.QTYGood == 0).
OrderBy(i => i.PartNumber).
Join(OtherSQLStatement(),
i => i.PartNumber,
o => o.PartNumber,
(i, o) =>
new Parts
{
PartNumber = i.PartNumber,
Description = i.Description,
Quantity = o.Quantity,
DeliveryDate = o.DeliveryDate
}).ToList();
And again: I assume you have PartNumber in both returned collections to identify which item belongs to which.
Edit
In this case the LINQ Query syntax would probably be more readable:
var joinedList = from aElem in a
join bElem in b
on aElem.PartNumber equals bElem.PartNumber into joinedAB
from abElem in joinedAB.DefaultIfEmpty()
select new Part
{
PartNumber = aElem.PartNumber,
Description = aElem.Description,
DeliveryDue = abElem == null ? null : abElem.DeliveryDue,
QtyOnOrder = abElem == null ? null : abElem.QtyOnOrder
};
Your DeliveryDue and QtyOnOrder are probably nullable. If not, replace the nulls by your default values. E.g. if you don't have the element in b and want QtyOnOrder to be 0 in the resulting list, change the line to
QtyOnOrder = abElem == null ? 0 : abElem.QtyOnOrder