Simple linq select query not returning correct results - linq

When I run the following query in SQL Server Management Studio I get 132 rows of data. This is correct.
select *
from cyc.contac con
where (con.note_turn is null) and
(con.cont_type <> '_') and
(con.cont_code <> 'D') and
(con.cont_type <> 'Q')
When I run the following Linq query from my code I get over 11000 records. This is not correct. I am wondering why my linq query is not constraining by the constraints in my where clause.
var query = from contac in context.CONTACs
orderby contac.ID
where (contac.note_turn == null) &&
(contac.cont_type != "_") &&
(contac.cont_code != "D") &&
(contac.cont_type != "Q")
select contac;

Related

SQL to LINQ convert

How can i convert from SQL as below to LINQ.
Select sum(detail.PO_Quantity) From PO_Master master
left join PO_Detail detail on master.ID_PO = detail.ID_PO
where master.PO_Status <> 21 And detail.PO_Item_Amount <> '0.00' And master.ID_PR = 'PR00010275'
Try this.
var result = (from master in PO_Master
join detail in PO_Detail on master.ID_PO equals detail.ID_PO into md
from masterDetail in md.DefaultIfEmpty()
where master.PO_STATUS != 21
&& masterDetail.Item_Amount != "0.00"
&& master.ID_PR == "PR00010275"
select masterDetail.Quantity).Sum();

SQL to LINQ with JOIN and SubQuery

I have a query that I' struggling to convert to LINQ. I just can't get my head around the required nesting. Here's the query in SQL (just freehand typed):
SELECT V.* FROM V
INNER JOIN VE ON V.ID = VE.V_ID
WHERE VE.USER_ID != #USER_ID
AND V.MAX > (SELECT COUNT(ID) FROM VE
WHERE VE.V_ID = V.ID AND VE.STATUS = 'SELECTED')
The Closest I've come to is this:
var query = from vac in _database.Vacancies
join e in _database.VacancyEngagements
on vac.Id equals e.VacancyId into va
from v in va.DefaultIfEmpty()
where vac.MaxRecruiters > (from ve in _database.VacancyEngagements
where ve.VacancyId == v.Id && ve.Status == Enums.VacanyEngagementStatus.ENGAGED
select ve).Count()
...which correctly resolves the subquery from my SQL statement. But I want to further restrict the returned V rows to only those where the current user does not have a related VE row.
I've realised that the SQL in the question was misleading and whilst it led to technically correct answers, they weren't what I was after. That's my fault for not reviewing the SQL properly so I apologise to #Andy B and #Ivan Stoev for the misleading post. Here's the LINQ that solved the problem for me. As stated in the post I needed to show vacancy rows where no linked vacancyEngagement rows existed. The ! operator provides ability to specify this with a subquery.
var query = from vac in _database.Vacancies
where !_database.VacancyEngagements.Any(ve => (ve.VacancyId == vac.Id && ve.UserId == user.Id))
&& vac.MaxRecruiters > (from ve in _database.VacancyEngagements
where ve.VacancyId == vac.Id && ve.Status == Enums.VacanyEngagementStatus.ENGAGED
select ve).Count()
This should work:
var filterOutUser = <userId you want to filter out>;
var query = from vac in _database.Vacancies
join e in _database.VacancyEngagements
on vac.Id equals e.VacancyId
where (e.UserId != filterOutUser) && vac.MaxRecruiters > (from ve in _database.VacancyEngagements
where ve.VacancyId == vac.Id && ve.Status == Enums.VacanyEngagementStatus.ENGAGED
select ve).Count()
select vac;
I removed the join to VacancyEngagements but if you need columns from that table you can add it back in.

Why does LINQ-to-SQL generate this SQL statement for a comparison

if I have a LINQ to SQL query like this:
var query = from x in db.XTable
select x.y == 123; // the y column is nullable!
List<bool> list = query.ToList();
it will generate a SQL statement that will contain this:
(CASE
WHEN [t0].[y] = 123 THEN 1
WHEN NOT ([t0].[y] = 123) THEN 0
ELSE NULL
END)
which will throw an error cause null could not get assigned to bool. I know why this happens (because a comparison in SQL with null is always false) but I don't know why LINQ to SQL does not use a stetement like this:
(CASE
WHEN [t0].[y] = 123 THEN 1
ELSE 0
END)
which would work.
Can I push LINQ to SQL to do this?
Probably, as you state, because you must think the SQL way with linq to sql, not the object way...
It might be considered as a bug or as a feature, by the way...
Especially with null values.
For example, concatenation of nullable string is different in linq to sql and linq to objects.
Assuming a and b are strings :
from n in db
select n.a + n.b
in linq to sql, if a is null and b is not, a + b = null
in linq to object if a is null anb b is not a + b = b
to get the same result in linq to sql, you'll have to use the coalesce operator select (a ?? string.Empty) + b
Anyway, you can either return a list of Nullable<bool> and a list of bool which would be :
from x in db.XTable
select x.y != null && x.y == 123
or
from x in db.XTable
select (x.y ?? 0) == 123
But to get what you want in linq to objects, you would have to do
from x in db.XTable.ToList()
select (x.y== null ? (bool?)null : x.y== 123))
EDIT
This might change in future versions (not sure if the given case will be included)

LINQ: A query body must end with a select clause or a group clause and other errors

I have the following SQL statement:
CASE
WHEN wt.pmtlr_oi IS NULL OR (SELECT COUNT(*) FROM mc.SCHEDENTRY WITH (NOLOCK) WHERE wt_oi = wt.wtskoi AND shedref_oi NOT IN (SELECT shdoi FROM mc.SCHDULE WITH (NOLOCK) WHERE aenm LIKE '%Breakin%')) = 0 AND wt.pmtlr_oi IS NULL
THEN 'Break-In'
ELSE 'Planned'
END AS schedule
which I have translated into:
Schedule = wt.pmtlr_oi == null || (from sch in SCHEDENTRies where wt_oi == wt.wtskoi && shedref_oi !(from sc in SCHDULEs
where sc.Aenm.Contains("Breakin") select sc.Shdoi)).Count() == 0 && wt.pmtlr_oi == null ? "Break-In" : "Planned"}
Here's how it looks in the select statement:
select new {Schedule = wt.pmtlr_oi == null || (from sch in SCHEDENTRies where wt_oi == wt.wtskoi && shedref_oi !(from sc in SCHDULEs
where sc.Aenm.Contains("Breakin") select sc.Shdoi)).Count() == 0 && wt.pmtlr_oi == null ? "Break-In" : "Planned"});
However when I try and run it there are some problems which I do not know how to fix. The first one is that the compiler doesn't appear to like the NOT IN found here:
&& shedref_oi !(from sc in SCHDULEs
I get a "A query body must end with a select clause or a group clause" error.
The compiler also doesn't like this:
select sc.Shdoi)).Count() == 0
On the outer parenthesis I get a "Syntax error ',' expected" error and on the period I get a "Statement expected" error message.
I would appreciate some help in troubleshooting this.
EDIT:
OK, After the comments I have corrected the LINQ statement to look like this:
select new {Schedule = wt.pmtlr_oi == null || (from sch in SCHEDENTRies where wt_oi == wt.wtskoi && !(from sc in SCHDULEs
where sc.Aenm.Contains("Breakin") select sc.Shdoi).Contains(shedref_oi)).Count() == 0 && wt.pmtlr_oi == null ? "Break-In" : "Planned"});
and the compiler likes it. However, it still doesn't like the .Count(). I get a "Statement expected" error. Am I incorrect in using .Count() for the sql SELECT COUNT(*)?

Convert this SQL query to Linq (Not Exists + sub query)

I would like this SQL to be converted to LINQ. (it shouldl select rows from input which do not exist in table production based on 3 columns. If a column in both tables contains NULL, it should be considered as having the same value)
SELECT i.* FROM INPUT AS i
WHERE NOT EXISTS
(SELECT p.Agent FROM Production AS p
WHERE ISNULL(i.CustID,'') <> ISNULL(p.CustID,'')
AND ISNULL(i.CustName,'') <> ISNULL(p.CustName,'')
AND ISNULL(i.household,'') <> ISNULL(p.Household,''))
First of all - this is not a good SQL query. Every column is wrapped in a non-sargable function which means that the engine won't be able to take advantage of any indexes on any of those columns (assuming you have any).
Let's start by rewriting this as a semi-decent SQL query:
SELECT i.*
FROM Input i
LEFT JOIN Production p
ON (p.CustID = i.CustID OR (p.CustID IS NULL AND i.CustID IS NULL))
AND (p.CustName = i.CustName OR (p.CustName IS NULL AND i.CustName IS NULL))
AND (p.Household = i.Household OR
(p.Household IS NULL AND i.Household IS NULL))
WHERE p.CustID IS NULL
Now having said this, LEFT JOIN / IS NULL is not great for efficiency either, but we don't have much choice here because we're comparing on multiple columns. Based on your column names, I'm starting to wonder if the schema is properly normalized. A CustID should most likely be associated with one and only one CustName - the fact that you have to compare both of these seems a bit odd. And Household - I'm not sure what that is, but if it's a varchar(x)/nvarchar(x) column then I wonder if it might also have a 1:1 relationship with the customer.
If I'm speculating too much here then feel free to dismiss this paragraph; but just in case, I want to say that if this data isn't properly normalized, normalizing it would make it much easier and faster to query on:
SELECT *
FROM Input
WHERE CustID NOT IN (SELECT CustID FROM Production)
Anyway, going back to the first query, since that's what we have to work with for now. Unfortunately it's impossible to create a join on those specific conditions in Linq, so we need to rewrite the SQL query as something slightly worse (because we now have to read from Input twice):
SELECT *
FROM Input
WHERE <Primary Key> NOT IN
(
SELECT i.<Primary Key>
FROM Input i
INNER JOIN Production p
ON (p.CustID = i.CustID OR (p.CustID IS NULL AND i.CustID IS NULL))
AND (p.CustName = i.CustName OR (p.CustName IS NULL AND i.CustName IS NULL))
AND (p.Household = i.Household OR
(p.Household IS NULL AND i.Household IS NULL))
)
Now we have something we can finally translate to Linq syntax. We still can't do the join explicitly, which would be best, but we go old-school, start from the cartesian join and toss the join conditions into the WHERE segment, and the server will still be able to sort it out:
var excluded =
from i in input
from p in production
where
((p.CustID == i.CustID) || ((p.CustID == null) && (i.CustID == null))) &&
((p.CustName == i.CustName) ||
((p.CustName == null) && (i.CustName == null))) &&
((p.Household == i.Household) ||
((p.Household == null) && (i.Household == null)));
select i.PrimaryKey;
var results =
from i in input
where !excluded.Contains(i.PrimaryKey)
select i;
I'm assuming here that you have some sort of primary key on the table. If you don't, you've got other problems, but you can get around this particular problem using EXCEPT:
var excluded =
from i in input
from p in production
where
((p.CustID == i.CustID) || ((p.CustID == null) && (i.CustID == null))) &&
((p.CustName == i.CustName) ||
((p.CustName == null) && (i.CustName == null))) &&
((p.Household == i.Household) ||
((p.Household == null) && (i.Household == null)));
select i;
var results = input.Except(excluded);

Resources