I want to use select query using linq to list.
in fallowing code userDtls is an list & Common.UserRightVal is an enum variable
userDtls = _userService.GetById(id);
var permission = from udtls in userDtls[0].userRightList
where udtls.object_name == "Person" &&
(udtls.user_right == (int)Common.UserRightVal.update
|| udtls.user_right == (int)Common.UserRightVal.add_update_delete_read
|| udtls.user_right == (int) Common.UserRightVal.allRight)select udtls;
Here I want to check permission details for user. My question is if 'where' condition is getting false then also permission variable is showing default records as shown by userDtls list.
Thanks to every one for response. I got answer, here it is.
var permission=(dynamic) null;
permission = (from udtls in userDtls[0].userRightList
where udtls.object_name.Contains(objectType) &&
(udtls.user_right == (int)Common.UserRightVal.readAll
|| udtls.user_right == (int)Common.UserRightVal.read_readAll
|| udtls.user_right == (int)Common.UserRightVal.add_update_delete_read
|| udtls.user_right == (int)Common.UserRightVal.allRight)
select udtls).FirstOrDefault();
Related
I need to run multiple conditions in linq query with or, and operator. First I need to compare with three conditions, which are separated by || operator, once one of the conditions is true I also need to see if check is true which I using && operator but I got all the result
so for example from following query, if ModuleLead == ContextSession.StaffID then record must have ClinicalSupervisorCheck == true
var query_b = (from b in activeAssessmentWithRemidiation
where b.AssessorID == ContextSession.StaffID
|| b.ModuleLead == ContextSession.StaffID
&& b.ClinicalSupervisorCheck == true
|| b.SeniorStaffID == ContextSession.StaffID
&& b.ModuleLeadCheck==true
select b).ToList();
the above query is running on following table records
It could be worth wrapping the AND conditions in parathensis, for example:
var query_b = (from b in activeAssessmentWithRemidiation
where b.AssessorID == ContextSession.StaffID ||
(b.ModuleLead == ContextSession.StaffID && b.ClinicalSupervisorCheck == true) ||
(b.SeniorStaffID == ContextSession.StaffID && b.ModuleLeadCheck==true)
select b).ToList();
Use parenthesis for that. Remember that operator && will be tested before || operators if you don't use parenthesis.
proper use of ( and )
var query_b = (from b in activeAssessmentWithRemidiation
where (((b.AssessorID == ContextSession.StaffID
|| b.ModuleLead == ContextSession.StaffID )
&& b.ClinicalSupervisorCheck == true)
||
(b.SeniorStaffID == ContextSession.StaffID
&& b.ModuleLeadCheck==true))
select b).ToList();
I have a ternary operator as below for a LINQ query as shown
var sub = (SubordinationType == 1) ? (true&false) : false;
var query = from vw in dbContext.vw
where (vw.office == FieldOffice && vw.SubAgreement == sub)
select vw;
Here SubAgreement is a bit field in database I need to select both true and false(0,1) or false(0) based on the ternery how do i achieve this?
Any quick suggestions please.
I think I get your question. Your logic is:
If Subordination is 1, SubAgreement doesn't matter (true or false)
If Subordination is not 1, SubAgreement should be false
hence add another condition:
//..
where (vw.office == FieldOffice && (Subordination == 1 || !vw.SubAgreement))
Try something like:
var query = from vw in dbContext.vw
where vw.office == FieldOffice
select vw;
if (SubordinationType != 1)
{
query = query.Where(vw => vw.SubAgreement == false);
}
In LINQ it's very easy to add new where clauses that are in && with the other clauses (note that it's only easy to add if you want them to be in &&, the || case is much more complex! :-) )
You can make a condition that is true when SubordinationType is 1 or when SubAgreement is false:
var query = from vw in dbContext.vw
where (vw.office == FieldOffice && (SubordinationType == 1 || vw.SubAgreement == false))
select vw;
Is it possible to use a Linq query to search through a List? In my web app I have to potentially process over 14k records based upon a spreadsheet uploaded by the user. With each record processed, I need to compare that record against what we currently have in our database in order to make sure we either aren't adding a duplicate or I know what record I need to be updating/editing.
Instead of hitting the database 14k times or more, I wanted to pull all the records contained on this table into a List, and then perform a search based on a set of conditions.
Here is the Linq query I currently have that hits the database. The business rules are pretty... complicated so I won't bother you with the details but these are the conditions that I need to satisfy for the search. I've tested this query and it returns the expected results.
var previousZips = (from z in db.ZipCodeTerritory
where (item.ZipCode.Equals(null) ?
z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
SqlFunctions.DateDiff("DAY", z.EndDate, item.EndDate) == 0 :
z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
SqlFunctions.DateDiff("DAY", z.EndDate, item.EndDate) == 0 &&
(z.ZipCode.Equals(null) || z.ZipCode.Equals(item.ZipCode)))
select z).ToList();
What I would like to do, however, is create a List of all the records on the table like this:
List<ZipCodeTerritory> allRecords = (from z in db.ZipCodeTerritory
select z).ToList()
and then use a query similar to this to pull the record I'm looking for from the list:
List<ZipCodeTerritory> previousZips = allRecords.Where(
z => (item.ZipCode.Equals(null)
? z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
SqlFunctions.DateDiff("DAY", z.EndDate,
item.EndDate) == 0
: z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
SqlFunctions.DateDiff("DAY", z.EndDate,item.EndDate) == 0 &&
(z.ZipCode.Equals(null) || z.ZipCode.Equals(item.ZipCode))
)
).ToList();
The query above (from the List), however, throws the following error:
This function can only be invoked from LINQ to Entities.
Answered my own question. The problem here was the SqlFunction. By removing that from the query and re-writing it like this it works
List<ZipCodeTerritory> previousZips = allRecords.Where(
z => (item.ZipCode.Equals(null)
? z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
z.EndDate.Date == item.EndDate.Date
: z.StateCode.Equals(item.StateCode) &&
z.ChannelCode.Equals(item.ChannelCode) &&
z.EndDate.Date == item.EndDate.Date &&
(z.ZipCode.Equals(null) || z.ZipCode.Equals(item.ZipCode))
)
).ToList();
Instead of
SqlFunctions.DateDiff("DAY", z.EndDate,item.EndDate) == 0
use
z.EndDate.Subtract(item.EndDate).TotalDays == 0
I am getting the wrong count error and I can't figure out why.
In my database serverId = 1/2/3 has only one started value and 3 notstarted value for each server.
painfo = (from paes in server.AppPM_Paes
where (paes.PaStatus == "Started" || paes.PaStatus == "NotStarted" ) && paes.ServerId != null
select new PaDetails { ServerID = paes.ServerId, PaStatus = paes.PaStatus }).ToList();
foreach (PaDetails a in painfo)
{
if (a.PaStatus.Contains("Started") && a.ServerID.Equals(1))
stCount1++;
if (a.PaStatus.Contains("Started") && a.ServerID.Equals(2))
stCount2++;
if (a.PaStatus.Contains("Started") && a.ServerID.Equals(3))
stCount3++;
if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(1))
notStCount1++;
if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(2))
notStCount2++;
if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(3))
notStCount3++;
}
But in my above code (stCount#->started count), stCount# has the value 4 instead of 1.
What's wrong in my code?
Can you please help me?
You're doing a .Contains("Started"). This will include any string that has the word "Started" in it, including "NotStarted"
You can change it to a.PaStatus == "Started" or a.PaStatus.Equals("Started")
Hi I'm having a problem with getting a conditional query to work. I want all projects where project.Parent either is null or if it has a parent then it shouldn't be voided or closed.
My example will NOT bring back any projects where project.Parent == null.
We are using linq-to-nhibernate
var projects = (from project in this.Session.Query<Project>()
where project.IsClosed == false
&& project.IsVoided == false
&& (project.Parent == null
|| (project.Parent.IsVoided == false
&& project.Parent.IsClosed == false))
select project).ToList();
That query won't work because inner joins are generated for the Parent property.
The easiest workaround is doing two queries and joining them client-side:
var projects = (from project in this.Session.Query<Project>()
where project.IsClosed == false
&& project.IsVoided == false
&& project.Parent == null
select project)
.AsEnumerable()
.Concat(
(from project in this.Session.Query<Project>()
where project.IsClosed == false
&& project.IsVoided == false
&& project.Parent.IsVoided == false
&& project.Parent.IsClosed == false
select project))
.ToList();
I'd suggest to fetch all project and to check what happens to projects that should be null. Without having any example data etc. I have to guess what is causing the problem. I'd say the project parents are initialized with some empty state.
Doing the join client-side is not required:
var projects = (from project in this.Session.Query<Project>()
where project.Parent == null || (project.IsClosed == false
&& project.IsVoided == false)
&& (project.Parent == null
|| (project.Parent.IsVoided == false
&& project.Parent.IsClosed == false))
select project).ToList();