I wanting to know the best approach for a where clause with less than or equal to where the value to match is int?
var outOfStockProducts = (from theMapProd in context.tblProductOptions_MAP
join theProd in context.tblProducts on theMapProd.productID equals theProd.productID
where theProd.stock_Level <= 5
select theMapProd).ToList();
This is another way,
Not tested.
var outOfStockProducts = (from theMapProd in context.tblProductOptions_MAP
join theProd in context.tblProducts on theMapProd.productID equals theProd.productID
select theMapProd).ToList();
outOfStockProducts=outOfStockProducts.where(x=>x.stock_Level < 5 || x.stock_Level ==5).ToList();
Related
If the person you are searching is a CIA emplyee, take his CIAJobs.EmployerID, otherwise select People.ID
SELECT
case when CIAJobs.EmployeeID IS NULL then People.ID
else CIAJobs.EmployerID
end
FROM [FMO].[People] AS p
LEFT JOIN [FMO].[CIAJobs] j
ON (p.ID = j.[EmployeeID])
AND (j.[relationshipType] = '25a8d79d-377e-4108-8c92-0ef9a2e1ab63')
where p.ID = '1b66e032-94b2-e811-96e0-f48c508e38a2' // id of person you search for
OR
j.[EmployeeID] = '1b66e032-94b2-e811-96e0-f48c508e38a2' // id of person you search for
I tried doing this in Linq:
var a = from l in People
join x in CIAJobs
on l.Id equals x.EmployeeID && x.RelationshipTypeGuid equals Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')
into gcomplex
from xx in gcomplex.DefaultIfEmpty()
select (xx.EmployeeID == null) ? l.EmployeeId : x.EmployerID;
var b = a.ToList();
why does the query show an error because of this chunk: && x.RelationshipTypeGuid equals Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')
If I remove this part it shows no error.
Error is: operator && cannot be applied to operands of type Guid and Guid.
Can you help me correct the Linq query please logically and syntactically? Thank you.
You don't need join for multiple conditions in this scenario. Use this
var a = from l in People
join x in CIAJobs
.Where(z=>z.RelationshipTypeGuid
.Equals(Guid.Parse('25a8d79d-377e-4108-8c92-0ef9a2e1ab63')))
on l.Id equals x.EmployeeID
into gcomplex
from xx in gcomplex.DefaultIfEmpty()
select (xx.EmployeeID == null) ? l.EmployeeId : x.EmployerID;
var b = a.ToList();
But based on your problem statement this should do
var a = from l in People
join x in CIAJobs
on l.Id equals x.EmployeeID
into gcomplex
from xx in gcomplex.DefaultIfEmpty()
select (xx == null) ? l.EmployeeId : xx.EmployerID;
var b = a.ToList();
I want to calculate a Sum value using fields from 2 tables and can't work out the syntax.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select new {
lessonCost = (t.Rate) * (l.Duration / 60)
});
This returns a set of values for individual lessons. How do I get the Sum total of these lessons?
You are selecting a new IEnumerable of an anonymous object type. Just select the correct value you are looking for. Then you have IEnumerable of float/double/decimal/whatever. Then take the resulting sum of that query.
var x = (from l in context.Lessons
join t in context.Tariffs on l.TariffId equals t.Id
where l.StudentSemesterId == studentSemesterId
select (t.Rate) * (l.Duration /60)).Sum();
I am trying to write a LINQ query that counts how many times <p> and </p> shows up.
from d in IPACS_Documents
join dp in IPACS_ProcedureDocs on d.DocumentID equals dp.DocumentID
join p in IPACS_Procedures on dp.ProcedureID equals p.ProcedureID
where d.DocumentID == 4
& d.DateDeleted == null
select ??
The select is where I am stuck. The column I need to count how many times <p> is in d.Html the same for </p>
I'm not sure there's a Linq-To-SQL/Entities way to complete this, since SQL doesn't do this easily.
var result = (from d in IPACS_Documents
join dp in IPACS_ProcedureDocs on d.DocumentID equals dp.DocumentID
join p in IPACS_Procedures on dp.ProcedureID equals p.ProcedureID
where d.DocumentID == 4
&& d.DateDeleted == null
select d.Html).First();
int count = Regex.Matches(result, "<p>|</p>").Count;
will probably do it.
Linq is brand new to me so I apologize if this is really stupid.
I am trying to get the count from a multi-table join with where clause, without group by. I've seen examples of group by and will resort to that if need be, but I am wondering if there is a way to avoid it. Is sql my query would look something like this;
SELECT Count(*)
FROM plans p
JOIN organizations o
ON p.org_id = o.org_id
AND o.deleted IS NULL
JOIN orgdata od
ON od.org_id = o.org_id
AND od.active = 1
JOIN orgsys os
ON os.sys_id = od.sys_id
AND os.deleted IS NULL
WHERE p.deleted IS NULL
AND os.name NOT IN ( 'xxxx', 'yyyy', 'zzzz' )
What's the best way to get this?
All you need is to call Count(). You're only counting the number of results. So something like:
var names = new[] { "xxxx", "yyyy", "zzzz" };
var query = from plan in db.Plans
where plan.Deleted == null
join organization in db.Organizations
on plan.OrganizationId equals organization.OrganizationId
where organization.Deleted == null
join orgData in db.OrganizationData
on organization.OrganizationId equals orgData.OrganizationId
where orgData.Active == 1
join os on db.OrganizationSystems
on orgData.SystemId equals os.SystemId
where os.Deleted == null &&
!names.Contains(os.Name)
select 1; // It doesn't matter what you select here
var count = query.Count();
In LINQ, I write a query for data, which is between two dates. In this case I am not getting data. When user enters same date as from date and to date the data is not displaying while there are data for that particular date.
This is my LINQ query.
var result = from MaterialStatus in materialRequistionEntities.Tbl_MatReq_NewItemReq_M
join CurrentStatus in materialRequistionEntities.Tbl_StatusMaster_M
on MaterialStatus.ReqCurStatus equals CurrentStatus.StatusId
join Employee in materialRequistionEntities.Tbl_Emp_Master_M
on MaterialStatus.Indentor equals Employee.EmpId
where (MaterialStatus.CreatedOn >= dt.Date
&& MaterialStatus.CreatedOn <= dt1.Date)
select *****************
You should do:
var upperDate = dt1.Date.AddDays(1);
...
where (MaterialStatus.CreatedOn >= dt.Date
&& MaterialStatus.CreatedOn < upperDate
This way the expression can be converted to a sargable SQL predicate.
Assuming that dt1 is in reality greater that dt, you could use
var result = from MaterialStatus in materialRequistionEntities.Tbl_MatReq_NewItemReq_M
join CurrentStatus in materialRequistionEntities.Tbl_StatusMaster_M on
MaterialStatus.ReqCurStatus equals CurrentStatus.StatusId
join Employee in materialRequistionEntities.Tbl_Emp_Master_M
on MaterialStatus.Indentor equals Employee.EmpId
where (MaterialStatus.CreatedOn.Date >= dt.Date && MaterialStatus.CreatedOn.Date <= dt1.Date)
select *****************
Notice CreatedOn.Date to deal with the hour/minute/seconds/milliseconds components
I hope it will be ok.
where (MaterialStatus.CreatedOn.Value.Date >= dt.Date && MaterialStatus.CreatedOn.Value.Date <= dt1.Date.AddDay(1))