LINQ Query Incorrect Results Using AND and AND OR - c#

I have a linq query which is pulling in the wrong results. My guess is that it's doing this because I'm missing a bracket or something or another.
var CurrentlyActiveIncidents = from b in db.Logs
join dc in db.Incidents
on b.LogID equals dc.LogID
where b.DateTimeResolved == null &&
(dc.AssignedTechnician != null ||
dc.AssignedTechnician != CurrentUser)
I want all the incidents where the datetimeresolved == null and the assigned technician isn't null or is not assigned to the current user. At the moment the query is ignoring the != currentuser part and I don't know why?

As you require all those records which has assigned technician != null and != current user
so you need to have And Condition
var CurrentlyActiveIncidents = from b in db.Logs
join dc in db.Incidents
on b.LogID equals dc.LogID
where b.DateTimeResolved == null &&
(dc.AssignedTechnician != null &&
dc.AssignedTechnician != CurrentUser)
this will give you required records

Related

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.

how to pull a null check for `item.GetType().GetProperty(getPropertyStringParameter)` in linq

how to put a check for null like,
if(item.GetType().GetProperty(getPropertyStringParameter) != null) for below linq query in C#?
matchingConfigItem = (from item in contextEnum
where item.GetType().GetProperty(getPropertyStringParameter).GetValue(item)
.Equals(configItem.GetType().GetProperty(getPropertyStringParameter)
?.GetValue(configItem))
select item).SingleOrDefault();
matchingConfigItem = (
from item in contextEnum
let itemProp = item.GetType().GetProperty(getPropertyStringParameter)
let configProp = configItem.GetType().GetProperty(getPropertyStringParameter)
where itemProp != null && configProp != null && itemProp.GetValue(item)
.Equals(configProp.GetValue(configItem))
select item
)
.SingleOrDefault();

C# Linq Left Outer Join WHERE clause

I have a linq query where i'm trying to return data if the right Sales table doesn't have a record and also only if the Sales.Fallthrough == false && Sales.Date == null.
This is the base of my query and i've tried many different things with this but can't seem to get it to return the required data. Everything I try only seems to return records if there is no Sales OR they match the WERE caluse but not both.
from cr in efContext.Cases
join si in efContext.Sales on cr.CaseId equals si.CaseId into sicr
from sicr in (from si in sicr
where si == null
select si
).DefaultIfEmpty()
where cr.Withdrawn == false
select new
{
CaseId = cr.CaseId,
PropertyAddress = extension.PropertyAddressTownFormat(cr.PropertyAddress1, cr.PropertyTown),
TargetExchangeDate = sicr.TargetExchangeDate == null ? null : sicr1.TargetExchangeDate,
ActualExchangeDate = sicr.ActualExchangeDate,
}).ToList();
EDIT
Missed out a little information.
Also if there is a record in Sales but does not match WHERE clauses then still return the left side and the right side as if there wasn't a record.
Thanks.
Isn't it just...?
from cr in efContext.Cases
join si in efContext.Sales on cr.CaseId equals si.CaseId into sicr
from s in sicr.DefaultIfEmpty()
select new
{
CaseId = cr.CaseId,
PropertyAddress = extension.PropertyAddressTownFormat(cr.PropertyAddress1, cr.PropertyTown),
TargetExchangeDate = s == null || (s.Fallthrough == false && s.Date == null) ? null : s.TargetExchangeDate,
ActualExchangeDate = s == null || (s.Fallthrough == false && s.Date == null) ? null : s.ActualExchangeDate,
};
Please note that you cannot call to ToList since the query returns an anonymous type.

C# Linq statement to join two tables and multiple columns

I have two tables; EndToEnd and PartPort. I'd like to get the PartPortA and PartportB data from the same row in EndToEnd, and query Partport with them and get their corresponding PartGid from Partport which could be on any row in the Partport table. So far, I'm able to do this, but I have to do two different LINQ calls but I'd like to reduce it down to one. Here is my code:
// this demonstrates how to join two tables, however only works for one AssetportGid at a time
var part_portGid_a_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortA equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
var part_portGid_b_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortB equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
return Json(part_portGid_a_results, JsonRequestBehavior.AllowGet);
What i'd like to do, and I have already tried but got an error is this:
var part_portGid_a_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
join ica in entities.PartPort
on icp.PartPortA && icp.PartPortB equals ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();
The error i get is:
Guid? EndToEnd.PartPortB
Error:
Operator '&&' cannot be applied to operands of type 'System.Guid' and 'System.Guid?'
You don't have to use join. If you want to join with a "complex" comparison, just make a Cartesian product (from ... from) and link the rows by a where clause
var part_portGid_results = (from icp in entities.EndToEnd
where icp.IntertPortGidA != null &&
icp.IntertPortGidB != null
from ica in entities.PartPort
where icp.PartPortA == ica.PortGid
|| icp.PartPortB == ica.PortGid
select new { icp.PartPortA, ica.PartGid, }).ToList();

How to check in a linq query for null?

I am trying in a linq query to check if one of the fields is null but i get this error whatever i do.
"Non-static method requires a target."
This is my code :
var users = from s in db.Users
where s.DepartmentId == booking.Item.DepartmentId && s.UserEmail != null
select s;
is any way to go through this error and be able to actually check if UserEmail is null?
p.s : i am using asp.net mvc entity framework.
Be sure that the first 2 letters are uppercase (DBNull.Value).
EDIT:
Try to copy your booking item into a local variable.
var departmentId = booking.Item.DepartmentId;
var users = from s in db.Users
where s.DepartmentId == departmentId && s.UserEmail != null
select s;
var users = from s in db.Users
where s.DepartmentId == booking.Item.DepartmentId && s.UserEmail != DbNull.Value
select s;
You need to compare it to DbNull.Value

Categories

Resources