I have this Linq query.I want to show ab.InTime and DbFunctions value together
var sdv = (from ab in db.Attendances
where ab.Employee == 63 && ab.InTime.Value.Year == 2015 && ab.InTime.Value.Month == 8
select ab.InTime,DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60);
Maybe, you can use select new{...}. Please try this:
var sdv = (from ab in db.Attendances
where ab.Employee == 63 && ab.InTime.Value.Year == 2015 && ab.InTime.Value.Month == 8
select new{
InTime=ab.InTime,
Minutes=DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60)
};
Related
I'm trying to convert the following SQL query to LINQ, but getting different result count with both,
SQL Query:
SELECT T5.CNTR, T5.BenefitCode,T5.ApprovedFlag,
T5.PaymentFrequencyCode, T5.InstalmentAmt, T5.TotalAmt,
T5.CarRego
FROM
dbo.EmployeeBenefit As T5
LEFT JOIN dbo.Payee ON T5.PayeeCntr = dbo.Payee.CNTR
LEFT JOIN dbo.BankDetails ON dbo.Payee.BankCntr = dbo.BankDetails.BankCntr
Left Join dbo.EmployeeCar As T4 on T5.EmployeeCarCntr=T4.Cntr
Inner Join dbo.EmployeeEntity As T1 On T5.EmployeeEntityCntr=T1.EmployeeEntityCntr
Inner Join dbo.EmployerEntity As T2 On T1.EmployerEntityCntr=T2.EmployerEntityCntr
where T5.EmployeeCntr = 117165
AND ((T5.EndDate is Null) OR (T5.EndDate >= GETDATE()))
LINQ:
var result = (from employeeBenefit in context.EmployeeBenefit
from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from bankDetails in context.BankDetails.Where(x => x.BankCntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from employeeCar in context.EmployeeCar.Where(x => x.Cntr == payee.BankCntr).DefaultIfEmpty()
from employeeEntity in context.EmployeeEntity
where employeeEntity.EmployeeEntityCntr == employeeBenefit.EmployeeEntityCntr
from employeeEntity1 in context.EmployeeEntity
where employeeEntity.EmployerEntityCntr == employeeEntity1.EmployerEntityCntr
&& employeeBenefit.EmployeeCntr == iEmployeeID
&& (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
&& employeeBenefit.EmployeeCntr == 117165
&& employeeBenefit.CarRego == registration
select new
{
CNTR = employeeBenefit.Cntr,
BenefitCode = employeeBenefit.BenefitCode,
PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
InstalmentAmount = employeeBenefit.InstalmentAmt,
TotalAmount = employeeBenefit.TotalAmt,
CarRego = employeeBenefit.CarRego,
ApprovedFlag = employeeBenefit.ApprovedFlag
}).ToList();
Please let me know what i'm missing.
For the data in my database the SQL query is returning 10 records. But, the LINQ is returning 2700 records.
Not a full answer (I'm late for work) but:
var result = (from T5 in context.EmployeeBenefit
join PY in dbo.Payee on T5.PayeeCntr equals PY.CNTR into PY1
where T5.EmployeeCntr = 117165
select new {
CNTR = T5.Cntr,
...
}
).ToList();
It was the issue with the condition mismatch that i had done in the LINQ. The below query just worked fine. Thank you for helping me with the issue.
var result = (from employeeBenefit in context.EmployeeBenefit
from payee in context.Payee.Where(x => x.Cntr == employeeBenefit.PayeeCntr).DefaultIfEmpty()
from bankDetails in context.BankDetails.Where(x => x.BankCntr == payee.BankCntr).DefaultIfEmpty()
from employeeCar in context.EmployeeCar.Where(x => x.Cntr == employeeBenefit.EmployeeCarCntr).DefaultIfEmpty()
join employeeEntity in context.EmployeeEntity
on employeeBenefit.EmployeeEntityCntr equals employeeEntity.EmployeeEntityCntr
join employerEntity in context.EmployerEntity
on employeeEntity.EmployerEntityCntr equals employerEntity.EmployerEntityCntr
where employeeBenefit.EmployeeCntr == 117165 && (!employeeBenefit.EndDate.HasValue || employeeBenefit.EndDate >= DateTime.Now)
select new
{
CNTR = employeeBenefit.Cntr,
BenefitCode = employeeBenefit.BenefitCode,
PaymentFrequencyCode = employeeBenefit.PaymentFrequencyCode,
InstalmentAmount = employeeBenefit.InstalmentAmt,
TotalAmount = employeeBenefit.TotalAmt,
CarRego = employeeBenefit.CarRego,
ApprovedFlag = employeeBenefit.ApprovedFlag
}).ToList();
I am new to Linq joins.
i have a sql query as below. Please help me convert the same to linq.
select c.mem_id,
c.po_start,
c.po_end,
isnull(pp.new_policy_code,c.policy_no)
from claims_data c
left join tob_policy tp on (c.subgroup_id =tp.subgroup_id and c.category_id = tp.category_id
and c.service_from_date Between tp.Start_Date And isnull (tp.End_Date,
cast(GETDATE() as date)))
Left Join Tob_Policy_Period Pp On (Pp.Policy_Id = tp.Policy_Id And
c.service_from_date Between Pp.Start_Date And Pp.End_Date )
where c.cid = 13
and c.g_id = 19013
and c.mem_id = '123'
and c.code ='555'
Try with this code:
var results = (from c in yourmodelentity.claims_data
join tp in yourmodelentity.tob_policy on c.subgroup_id equals tp.subgroup_id && c.category_id equals tp.category_id && c.service_from_date Between tp.Start_Date && isnull (tp.End_Date, datetime.now)))
join Pp in yourmodelentity.Tob_Policy_Period on Pp.Policy_Id equals tp.Policy_Id && c.service_from_date Between Pp.Start_Date && Pp.End_Date
where c.cid = 13 && c.g_id = 19013 && c.mem_id = '123' && c.code ='555'
select new
{
c.mem_id,
c.po_start,
c.po_end
}).ToList();
Try with below code:
var results = (from c in modelentity.claims_data
join tp in modelentity.tob_policy on new { first = c.subgroup_id, second= c.category_id } equals new { first = tp.subgroup_id, second = tp.category_id } into tpjoin
from tplj in tpjoin.DefaultIfEmpty()
join pp in modelentity.Tob_Policy_Period on new { first = tplj.Policy_Id ?? 0 } equals new { first = Pp.Policy_Id } into ppjoin
from pplj in tpjoin.DefaultIfEmpty()
where ((c.service_from_date) >= tplj.Start_Date && (c.service_from_date) <= (tplj.End_Date ?? DateTime.Now)) &&
((c.service_from_date) >= pplj.Start_Date && (c.service_from_date) <= pplj.End_Date) &&
(c.cid = 13 && c.g_id = 19013 && c.mem_id = "123" && c.code = "555")
select new
{
c.mem_id,
c.po_start,
c.po_end,
policy_no = (pplj != null ? (pplj.new_policy_code ?? c.policy_no) : c.policy_no)
}).ToList();
All:
Lets say I have the following table:
RevisionID, Project_ID, Count, Changed_Date
1 2 4 01/01/2016: 01:02:01
2 2 7 01/01/2016: 01:03:01
3 2 8 01/01/2016: 01:04:01
4 2 3 01/01/2016: 01:05:01
5 2 15 01/01/2016: 01:06:01
I am ordering the records based on Updated_Date. A user comes into my site and edits record (RevisionID = 3). For various reasons, using LINQ (with entity framework), I need to get the previous record in the table, which would be RevisionID = 2 so I can perform calculations on "Count". If user went to edit record (RevisionID = 4), I would need to select RevisionID = 3.
I currently have the following:
var x = _db.RevisionHistory
.Where(t => t.Project_ID == input.Project_ID)
.OrderBy(t => t.Changed_Date);
This works in finding the records based on the Project_ID, but how then do I select the record before?
I am trying to do the following, but in one LINQ statement, if possible.
var itemList = from t in _db.RevisionHistory
where t.Project_ID == input.Project_ID
orderby t.Changed_Date
select t;
int h = 0;
foreach (var entry in itemList)
{
if (entry.Revision_ID == input.Revision_ID)
{
break;
}
h = entry.Revision_ID;
}
var previousEntry = _db.RevisionHistory.Find(h);
Here is the correct single query equivalent of your code:
var previousEntry = (
from r1 in db.RevisionHistory
where r1.Project_ID == input.Project_ID && r1.Revision_ID == input.Revision_ID
from r2 in db.RevisionHistory
where r2.Project_ID == r1.Project_ID && r2.Changed_Date < r1.Changed_Date
orderby r2.Changed_Date descending
select r2
).FirstOrDefault();
which generates the following SQL query:
SELECT TOP (1)
[Project1].[Revision_ID] AS [Revision_ID],
[Project1].[Project_ID] AS [Project_ID],
[Project1].[Count] AS [Count],
[Project1].[Changed_Date] AS [Changed_Date]
FROM ( SELECT
[Extent2].[Revision_ID] AS [Revision_ID],
[Extent2].[Project_ID] AS [Project_ID],
[Extent2].[Count] AS [Count],
[Extent2].[Changed_Date] AS [Changed_Date]
FROM [dbo].[RevisionHistories] AS [Extent1]
INNER JOIN [dbo].[RevisionHistories] AS [Extent2] ON [Extent2].[Project_ID] = [Extent1].[Project_ID]
WHERE ([Extent1].[Project_ID] = #p__linq__0) AND ([Extent1].[Revision_ID] = #p__linq__1) AND ([Extent2].[Changed_Date] < [Extent1].[Changed_Date])
) AS [Project1]
ORDER BY [Project1].[Changed_Date] DESC
hope I understood what you want.
Try:
var x = _db.RevisionHistory
.FirstOrDefault(t => t.Project_ID == input.Project_ID && t.Revision_ID == input.Revision_ID -1)
Or, based on what you wrote, but edited:
_db.RevisionHistory
.Where(t => t.Project_ID == input.Project_ID)
.OrderBy(t => t.Changed_Date)
.TakeWhile(t => t.Revision_ID != input.Revision_ID)
.Last()
I have 2 tables:
'AllowedDates'
- DayID int PK
- Day datetime
'AllowedTimes'
- TimeID int PK
- DayID int FK
- Hour int
- Minute int
also I have table 'Users':
- ID int PK
- FirstName nvarchar(max)
...
and table 'UserDeniedTimes':
DayID int FK
UserID int FK
Hour int
Minute int
I need to select users, which don't have deny time (record in UserDeniedTimes) for concrete DayID/Hour/Minute
I try to do the following:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
&& a1.Hour == aTime.Hour
&& a1.Minute == aTime.Minute
))
)
select new ...
it works correctly, but with one exception. If user has record in UserDeniedTimes for some day, but another time, this user is not selected too. For example, UserDeniedTimes has record:
DayID = 10
UserID = 20
Hour = 14
Minute = 30
this user will be not selected if aTime has the following values:
DayID = 10
Hour = 9
Minute = 30
but will be selected if DayID = 11. Why?
[ADDED]
it works correctly when I limit only by day:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
))
)
select new ...
but not works when I write:
var result = from i in _dbContext.Users
where i.UserDeniedTimes.All(
p => (!p.AllowedDate.AllowedTimes.Any(
a1 => a1.Hour == 14
))
)
select new ...
why? What is difference between magic DayID and Hour ?
[ADDED #2]
((time == null) || i.UserDeniedTimes.All(p =>
//p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
does not work
((time == null) || i.UserDeniedTimes.All(p =>
p.AllowedDate.AllowedTimes.Any(a1 => a1.DayID != 33) &&
//(p.AllowedDate.AllowedTimes.Any(a2 => a2.Hour != 14)
))) &&
works
why?
Sometimes it helps to rephrase the problem: if I read you well, you don't want users, that have a deny time with at least one specified DayID/Hour/Minute:
where !i.UserDeniedTimes.Any(
p => (p.AllowedDate.AllowedTimes.Any(
a1 => a1.DayID == aTime.DayID
&& a1.Hour == aTime.Hour
&& a1.Minute == aTime.Minute
))
)
This should select the users you want. If not, please tell in some more words what exactly you
are trying to achieve.
convert this sqlserver corralate subquery to linq -
SELECT
ET2.[OID],
ET2.[EID],
ET2.[OrID],
ET2.ValidFromDate,
ET2.Quantity,
ET2.RAQID,
ET2.Threshold
FROM
[CDev].[Inf].[FEThreshold] AS ET2
WHERE
ET2.ValidFromDate =
(
SELECT
MAX(ET1.ValidFromDate)
FROM
[CDev].[Inf].[FEThreshold] AS ET1
WHERE
ET1.OID = ET2.OID
AND ET1.EID = ET2.EID
AND ET1.OrID = ET2.OrID
)
Maybe something like this:
var result=
(
from ET2 in db.FEThreshold
where
ET2.ValidFromDate=
(
from ET1 in db.FEThreshold
where ET1.OID == ET2.OID
&& ET1.EID == ET2.EID
&& ET1.OrID == ET2.OrID
select ET1.ValidFromDate
).Max()
select new
{
ET2.OID,
ET2.EID,
ET2.OrID,
ET2.ValidFromDate,
ET2.Quantity,
ET2.RAQID,
ET2.Threshold
}
);