How to select both boolean values or one - c#

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;

Related

FirstOrDefault LINQ query with a condition

I am new to LINQ queries and want to use FirstOrDefault in my existing LINQ query.
List<vUserAll> employee = (from o in db.vUsersAll
where (o.sAMAccountName == modifiedAccountName || o.CN == modifiedAccountName) && o.Domain == "dExample"
select o).ToList();
What's the correct way to do this?
This can be simplified further as:
var filtered = db.vUsersAll.FirstOrDefault(u => u. Field == filter);
If the above mentioned is the case, then you can use a labmda as in the following:
var firstOrDefaultResult = db.vUsersAll.Where(o=>
(o.sAMAccountName == modifiedAccountName || o.CN == modifiedAccountName)
&& o.Domain == "dExample").FirstOrDefault()
If you want to use the same above expression then,
vUserAll employee = (from o in db.vUsersAll
where (o.sAMAccountName == modifiedAccountName || o.CN == modifiedAccountName) && o.Domain == "dExample"
select o).FirstOrDefaul();
It's a lot easier if you only use LINQ extensions. For example,
var filtered = all users.Where(u => u. Field == filter).FirstOrDefault();

How to exclude from query's result in linq?

In the first picture I have the result of first query, the highlighted part indicates the rows that would be excluded by applying the filter on the second query, in the second I have the result of query select * from exlusion_table
I have to make a change to the first query to have it exclude the items retrieved from the second query
the first query:
var u = from a in cx.VW_LIST
where (a.PRJ == codPrj) && (a.DATE > date_ || a.DATE == null || date_ == null)
&& (x.Contains(a.CODE) || x.Count() == 0)
select a)
the second query:
var y = from esc in cx.EXCLUSION select esc
The first query should be modified to exclude all the rows that have the value fcode = the fcode of the second query (in the case in which the fscode of the second query = null) or that (fcode = fcode of the second query && fscode = fscode of the second query )
You can use Any(). ie:
var u = from a in cx.VW_LIST
where (a.PRJ == codPrj)
&& (a.DATE > date_ || a.DATE == null || date_ == null)
&& (x.Contains(a.CODE) || x.Count() == 0)
&& (!cx.EXCLUSION.Any( y => x.fscode == y.fscode && x.fcode == y.fcode ))
select a)
There are two approaches to this, one is to use ! and ANY() to filter out records found within the other list, this will compile into WHERE NOT EXISTS(_exclusion_) filter expression
var excluded = cx.EXCLUSION.AsQueryable();
var query = from vw in cx.VW_LIST
where vw.PRJ == codPrj
where vw.DATE == null || date_ == null || vw.DATE > date_
where !x.Any() || x.Contains(vw.CODE)
where !excluded.Any(esc => vw.fcode == esc.fcode
&& (esc.fscode == null || vw.fscode == esc.fscode))
select vw;
var results = query.ToList();
The tricky element is the null for fscode in the excluded table, that needs to act as wildcard match, or negate the fscode comparison.
It is not necessary to split the excluded query out into it's own query, we could have referenced to the cx.EXCLUSION table directly and it would have exactly the same effect, this shows you an encapsulation technique for building the LINQ query in a way that you could easily increase the complexity of the exclusion lookup without creating a mess of your overall query.
You may also find need to conditionally build the query, this is where fluent syntax provides a more modular approach:
bool filterExcludedRecords = true;
...
var excluded = cx.EXCLUSION.AsQueryable();
var query = cx.VW_LIST.Where(vw => vw.PRJ == codPrj)
.Where(vw => vw.DATE == null || date_ == null || vw.DATE > date_)
.Where(vw => !x.Any() || x.Contains(vw.CODE));
if(filterExcludedRecords)
query = query.Where(vw => !excluded.Any(esc => vw.fcode == esc.fcode
&& (esc.fscode == null || vw.fscode == esc.fscode)));
var results = query.ToList();
OUTER JOIN
Another method is to use a LFET OUTER JOIN where the exclusion match is not found:
var excluded = cx.EXCLUSION.AsQueryable();
var query = from vw in cx.VW_LIST
where vw.PRJ == codPrj
where vw.DATE == null || date_ == null || vw.DATE > date_
where !x.Any() || x.Contains(vw.CODE)
from esc in excluded.Where(e => vw.fcode == e.fcode
&& (e.fscode == null || vw.fscode == e.fscode))
.DefaultIfEmpty()
where esc.fscode == null
select vw;
var results = query.ToList();
The WHERE NOT EXISTS is often superior in terms of performance, OUTER JOIN may provide better response in an un-optimised table or when the number of rows in the exclusion list is significantly small and the number of rows in the main table is very large.
I include this query option for completeness, it is not well known that you can create simple outer joins by adding a new from clause to the query.

linq query with or and operator together

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();

Select based on where condition using linq

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();

Operator '!=' cannot be applied to operands of type 'bool?' and 'int'

I am getting the above error when I am trying to run my query in Linq Pad which is as below,
var qry = (from i in INTERNETDATAs
join c in COVERS on Convert.ToInt32(i.COVERTYPE) equals c.COV_TYPE
where i.DATELOADED >= new DateTime(2013,7,1)
&& i.DATELOADED < new DateTime(2013,8,1)
&& i.CAMPAIGNTYPE == "4"
&& c.COVERTYPEID == 17063789
&& c.CHILDAPPLIES != 1
&& c.SPOUSEAPPLIES != 1
select i
);
qry.Dump();
The column c.CHILDAPPLIES and c.SPOUSEAPPLIES in my COVERS table are of (bit, null) type.
My Sql query is working fine which as below but my LINQ query gives error Operator '!=' cannot be applied to operands of type 'bool?' and 'int'
SELECT Count(*)
FROM INTERNETDATA I Join COVERS C ON C.COV_TYPE = CONVERT(int, I.COVERTYPE)
WHERE ((I.DATELOADED >= CONVERT(DATETIME, '1 Jul 2013 00:00:00'))
AND (I.DATELOADED < CONVERT(DATETIME, '1 Aug 2013 00:00:00')))
AND (CONVERT(int, I.COVERTYPE) = C.COV_TYPE)
AND (C.COVERTYPEID = 17063789)
AND (I.CAMPAIGNTYPE = 4)
AND C.CHILDAPPLIES != 1
AND C.SPOUSEAPPLIES !=1
Thanks in advance.
I suspect you just want:
&& !c.CHILDAPPLIES
&& !c.SPOUSEAPPLIES
... although you should consider whether you want this to match rows where the columns are NULL rather than true or false. If the above doesn't compile (which it may not) you can effectively provide the "default" value with the null-coalescing operator:
&& !(c.CHILDAPPLIES ?? true)
&& !(c.SPOUSEAPPLIES ?? true)
or
&& !(c.CHILDAPPLIES ?? false)
&& !(c.SPOUSEAPPLIES ?? false)
The problem is that in the LINQ mapping, they're not "0 or 1" values - they're "true or false" values (or null) - so you can't compare them with integers.
try following i think this what you want
var qry = (from i in INTERNETDATAs
join c in COVERS on Convert.ToInt32(i.COVERTYPE) equals c.COV_TYPE
where i.DATELOADED >= new DateTime(2013,7,1)
&& i.DATELOADED < new DateTime(2013,8,1)
&& i.CAMPAIGNTYPE == "4"
&& c.COVERTYPEID == 17063789
&& c.CHILDAPPLIES != true
&& c.SPOUSEAPPLIES != true
select i
);
qry.Dump();
I would suggest it is one of these 2:
&& c.CHILDAPPLIES != 1
&& c.SPOUSEAPPLIES != 1
Even though they are of type Bit linq will compare them as boolean
If it returns bool? couldn't You just compare it with True or False?
Instead of CHecking it using '!=' Operator,Use
!c.CHILDAPPLIES && !c.SPOUSEAPPLIES.
This Might help

Categories

Resources