Rewrite sql query to LINQ. Can't find an error - c#

This is an SQL query:
SELECT Website,VendorID,Name,LinkProduct,
Link,Logo,Image,NameExtra as Industry,
(SELECT [Percent] FROM Web_Promotion
WHERE Web_Promotion.VendorID=Web_Vendor.VendorID)
AS PercentOff
FROM Web_Vendor WHERE Active='1' AND
(VendorID IN (Select VendorID FROM Web_Promotion
WHERE VendorID<>'' AND Static='True' AND [Percent] <> '0' AND
((Expires>=GETDATE()) OR (Expires IS NULL))) OR
VendorID IN (SELECT TOP 1 SC1 FROM NavItems
WHERE SC1=Web_Vendor.VendorID AND Promotion<>''
AND ((PromotionStart<=GETDATE() AND PromotionEnd>=GETDATE())
OR (PromotionStart<=GETDATE() AND PromotionEnd IS NULL))))
ORDER BY NameExtra,Sequence
I need to rewrite it to LINQ. So this is my LINQ:
return await _db.Web_Vendor.
Where(x => !(x.WebPromotion.VendorID == string.Empty || x.WebPromotion.VendorID == null)
&& x.WebPromotion.Static == true && x.WebPromotion.Percent != 0 &&
(x.WebPromotion.Expires >= DateTime.Now || x.WebPromotion.Expires == null)
||
(_db.NavItems.Where(y => x.WebPromotion.VendorID == y.SC1
&& !(y.Promotion == "" || y.Promotion == null)
&& (y.PromotionStart <= DateTime.Now) && (y.PromotionEnd >= DateTime.Now || y.PromotionEnd == null))
.Select(g => g.SC1).Take(1).Contains(x.WebPromotion.VendorID)))
.Include(x => x.WebPromotion).Where(x => x.Active == true).OrderBy(x => x.NameExtra)
.ThenBy(x => x.Sequence).ToListAsync();
I spent about three ours, but can't find an error. Original SQL query returns 16 rows, but my LINQ code returns only 13 of the. Unfortunately I have only one navigation property (Web_Vendor <-> Web_Promotion). I think that an error in the second part of my query:
||
(_db.NavItems.Where(y => x.WebPromotion.VendorID == y.SC1
&& !(y.Promotion == "" || y.Promotion == null)
&& (y.PromotionStart <= DateTime.Now) && (y.PromotionEnd >= DateTime.Now || y.PromotionEnd == null))
.Select(g => g.SC1).Take(1).Contains(x.WebPromotion.VendorID)))
Can any expert check my code and help me?
Correct data:
http://prntscr.com/9a5xwu
Linq data (not correct) contains the same data as correct instead of values where PercentOff is null.
The main problem is that LINQ generate inner join instead of left join in this place: http://prntscr.com/9a6stb

since you say that your linq data miss the case when PercentOff = null, i'd focus on that
I guess your "PercentOff" in Linq is Percent property, and i see that you have in your where: "x.WebPromotion.Percent != 0"
Is that a nullable value or you convert the null to the default property type, that is 0?
couldn't be that null is converted to 0 and then the query skip it?

Related

Cannot execute query because datetime won't be converted to string c#/MSSQL

I have this attempt to execute a query:
hmrSingle = dbMngr.Set<HistoryMessagesRequested>().Where(x => x.dateregistration == msgv.DtRicezione && x.val == msgv.value && x.parameter == dmf.Description && x.DeviceID == ms.IDDevice).FirstOrDefault();
dateregistration and DtRicezione are both declared as DateTime? into the database context and they're "datetime" into MSSQL tables too.
I can't figure out why this problem is continuously present.
Any time you are using a nullable value type you need to use .value to get its value. Try:
hmrSingle = dbMngr.Set<HistoryMessagesRequested>().Where(x => x.dateregistration.value == msgv.DtRicezione.value && x.val == msgv.value && x.parameter == dmf.Description && x.DeviceID == ms.IDDevice).FirstOrDefault();

Simplify LINQ query

I have the following query:
db.ObjectTags.Where(c =>
c.TagID == tagID &&
(!db.DeletedObjects.Any(d=> d.ForObjectTypeID == c.ForObjectTypeID && d.ForObjectID == c.ForObjectID)
|| !db.DeletedObjects.SingleOrDefault(d => d.ForObjectTypeID == c.ForObjectTypeID && d.ForObjectID == c.ForObjectID).Deleted)
)
Its goal is to return objects that are not in a deleted state.
The table DeletedObjects has two states:
A record doesn't exist (not deleted)
A record exists with a deleted (bool) value
I need to query where either the record doesn't exist, or if it does the deleted value is false.
Is there any way to condense that statement eg with SingleOrDefault()?
You only need one !db.DeletedObjects.Any(...) and no SingleOrDefault
var q = db.ObjectTags
.Where(c=> c.TagID == tagID && !db.DeletedObjects
.Any(d => d.Deleted && d.ForObjectTypeID == c.ForObjectTypeID && d.ForObjectID == c.ForObjectID));
Can you please try this linq query
db.ObjectTags.Where(c =>
c.TagID == tagID &&
(db.DeletedObjects.Any(d=> d.ForObjectTypeID == c.ForObjectTypeID && d.ForObjectID == c.ForObjectID && !c.Deleted))
)
I believe you need to left join between ObjectTags and DeletedObjects. A LINQ query like this:
from objectTag in db.ObjectTags
from deletedObject in db.DeletedObjects
.Where(deletedObject => deletedObject.ForObjectTypeID == objectTag.ForObjectTypeID && deletedObject.ForObjectID == objectTag.ForObjectID)
.DefaultIfEmpty()
where deletedObject == null || !deletedObject.Deleted

Optimize EF query using Any instead of contains

That was my query with Contains:
db.NavFilters.Where(finalExpression)
.Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink && db.NavItemsFilters
.Where(n=> !(n.Promo == string.Empty || n.Promo == null))
.Select(n=>n.ItemID)
.Contains(x.ItemID) )
But, as far as I know, contains is a hard operation, and I need to optimize it. Is such query will give the same result?
db.NavFilters.Where(x=> db.NavItemsFilters.Any(n=>n.Promo != string.Empty && n.ItemID == x.ItemID))
.Where(finalExpression)
.Count(x => x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink)
I know, that the best solution is to add navigation properties. But I can't do that for many reasons.
You can optimize you query like this:
var navFilters = db.NavFilters.Where(finalExpression);
var thereIsAny = (from x in navFilters
join n in db.NavItemsFilters on x.ItemID equals n.ItemID
where n.Promo != string.Empty && x.Attribute1 == e.Attribute && x.Link == link && x.SubLink == subLink
).Any();
IQueryable.Contains does exist, so your query should get converted to SQL.

Linq to SQL - Exact Relational Division

The Linq-to-SQL query below, currently results in:
NotSupportedException: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
Looks like the typical way to perform an exact relational division filter in linq is not available when using linq to SQL.
In the query below, we see the two attempts to use the Except clause to filter out any records that do not have IDs in the provided range.
Does anyone know what needs to be done besides changing to using a stored procedure or deferring the Expect filter to further filter on the returned list?
The section specifically is
&&
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(AdditionalServices.Where(ads => p.ReferenceNumber == ads.FK_Provider_ReferenceNumber).Select(ad => ad.FK_ServiceList_ID).Except(additionalServiceIDs).Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
&&
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(AdditionalNeeds.Where(adn => p.ReferenceNumber == adn.FK_Provider_ReferenceNumber).Select(an => an.FK_ServiceList_ID).Except(additionalNeedsIDs).Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
in :
List<int> additionalServiceIDs = new List<int>(){40,42};
List<int> establishmentIDs = new List<int>() {};
List<int> additionalNeedsIDs = new List<int>(){};
bool advertisedOnly = true;
bool Internal = true;
string providerTypeCode = "";
List<string> settingTowns = new List<string>() {};
var sqlResultsList = (from p in Providers
join po in ProviderOverviews on p.ReferenceNumber equals po.FK_Provider_ReferenceNumber into p_po
from p_po_LeftOuter in p_po.DefaultIfEmpty() // left outer join
join ad in AdditionalServices on p.ReferenceNumber equals ad.FK_Provider_ReferenceNumber into p_ad
from p_ad_LeftOuter in p_ad.DefaultIfEmpty() // left outer join
join sp in SchoolPickUps on p.ReferenceNumber equals sp.FK_Provider_ReferenceNumber into p_sp
from p_sp_LeftOuter in p_sp.DefaultIfEmpty() // left outer join
join an in AdditionalNeeds on p.ReferenceNumber equals an.FK_Provider_ReferenceNumber into p_an
from p_an_LeftOuter in p_an.DefaultIfEmpty() // left outer join
join il in Inspections on p.ReferenceNumber equals il.FK_Provider_ReferenceNumber into p_il
from p_il_LeftOuter in p_il.DefaultIfEmpty() // left outer join
join sl in SchoolLists on p_sp_LeftOuter.FK_SchoolList_SchoolID equals sl.SchoolID into sp_sl
from sp_sl_LeftOuter in sp_sl.DefaultIfEmpty() // left outer join
join nl in NeedLists on p_an_LeftOuter.FK_NeedsList_ID equals nl.ID into an_nl
from an_nl_LeftOuter in an_nl.DefaultIfEmpty() // left outer join
join svl in ServiceLists on p_ad_LeftOuter.FK_ServiceList_ID equals svl.ID into ad_svl
from ad_svl_LeftOuter in ad_svl.DefaultIfEmpty() // left outer join
join ptl in ProviderTypeLists on p.FK_ProviderTypeList_ID equals ptl.ID
where
(string.IsNullOrEmpty(providerTypeCode) || ptl.Code.ToLower() == providerTypeCode)
&&
p.Advertise == advertisedOnly
&&
(settingTowns == null || settingTowns.Count == 0 || settingTowns.Contains(p.SettingTown.ToLower())) // allow for no parameters being passed
&&
(establishmentIDs == null || establishmentIDs.Count == 0 || establishmentIDs.Contains(p_sp_LeftOuter.FK_SchoolList_SchoolID)) // allow for no parameters being passed
&&
(Internal == true || p.RegistrationStatus == "ACTV")// if internal = false then add filter .. RegistrationStatus = 'ACTV'
&&
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(AdditionalServices.Where(ads => p.ReferenceNumber == ads.FK_Provider_ReferenceNumber).Select(ad => ad.FK_ServiceList_ID).Except(additionalServiceIDs).Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
&&
(additionalNeedsIDs== null || additionalNeedsIDs.Count == 0 ||
!(AdditionalNeeds.Where(adn => p.ReferenceNumber == adn.FK_Provider_ReferenceNumber).Select(an => an.FK_NeedsList_ID).Except(additionalNeedsIDs).Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
select new // anonymous type
{
SettingTown = p.SettingTown,
ProviderTypeCode = ptl.Code,
ProviderName = p.ProviderName,
PublishedAddress = p_po_LeftOuter.PublishedAddressLocation,
Vacancies = p_po_LeftOuter.Vacancies,
VacancyMemo = p_po_LeftOuter.VacancyMemo,
PublishedPhone = p_po_LeftOuter.PublicPhone,
PublishedEmail = p_po_LeftOuter.PublicEmail,
Website = p_po_LeftOuter.Website,
AdditionalNeed = an_nl_LeftOuter.Description,
AdditionalNeedID = (int?)an_nl_LeftOuter.ID,
AdditionalService = ad_svl_LeftOuter.Description,
AdditionalServiceID = (int?)ad_svl_LeftOuter.ID,
CostPerDay = p_po_LeftOuter.CostPerDay,
CostPerHour = p_po_LeftOuter.CostPerHour,
CostPerSession = p_po_LeftOuter.CostPerSession,
Hours = p_po_LeftOuter.Hours,
InspectionOverallJudgement = p_il_LeftOuter.InspectionOVerallJudgement,
ReferenceNumber = p.ReferenceNumber,
ServiceDescription = p_po_LeftOuter.ServiceDescription,
SchoolPickUp = sp_sl_LeftOuter.SchoolName,
Under5 = p_po_LeftOuter.Under5,
Over5 = p_po_LeftOuter.Over5,
PublicTransport = p_po_LeftOuter.PublicTransport,
}).ToList();
The error message suggests that you can't use local sequence in LINQ to SQL except Contains() function. Based on that suggestion, try to avoid using .Except() and use .Contains() instead. You can replace this :
.Except(localCollection)
with this one :
.Where(!localCollection.Contains())
Example for your case :
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(AdditionalNeeds.Where(adn => p.ReferenceNumber == adn.FK_Provider_ReferenceNumber)
.Select(an => an.FK_ServiceList_ID)
.Where(fk => !additionalNeedsIDs.Contains(fk))
.Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
or simplified by merging both .Where()s :
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(AdditionalNeeds.Where(adn => p.ReferenceNumber == adn.FK_Provider_ReferenceNumber
&& !additionalNeedsIDs.Contains(adn.FK_ServiceList_ID))
.Select(an => an.FK_ServiceList_ID)
.Any())
) // *ONLY* records that match the provided list of additionalServiceIDs, no more - no less (aka. Exact Relational Division)
With help from #har07 I figured it out in entirety, the filter is thus.
&&
(additionalServiceIDs == null || additionalServiceIDs.Count == 0 ||
!(dbo.AdditionalServices.Where(ads => p_ad_LeftOuter.FK_Provider_ReferenceNumber == ads.FK_Provider_ReferenceNumber
&& !additionalServiceIDs.Contains(ads.FK_ServiceList_ID)
)
.Select(an => an.FK_ServiceList_ID)
.Any())
&&
p_ad_LeftOuter.FK_ServiceList_ID != null
&&
dbo.AdditionalServices.Where(ads => p_ad_LeftOuter.FK_Provider_ReferenceNumber == ads.FK_Provider_ReferenceNumber
&& additionalServiceIDs.Contains(ads.FK_ServiceList_ID))
.Select(an => an.FK_ServiceList_ID)
.Distinct()
.Count() == additionalServiceIDs.Count
)
&&
(additionalNeedsIDs == null || additionalNeedsIDs.Count == 0 ||
!(dbo.AdditionalNeeds.Where(ads => p_an_LeftOuter.FK_Provider_ReferenceNumber == ads.FK_Provider_ReferenceNumber
&& !additionalNeedsIDs.Contains(ads.FK_NeedsList_ID)
)
.Select(an => an.FK_NeedsList_ID)
.Any())
&&
p_an_LeftOuter.FK_NeedsList_ID != null
&&
dbo.AdditionalNeeds.Where(ads => p_an_LeftOuter.FK_Provider_ReferenceNumber == ads.FK_Provider_ReferenceNumber
&& additionalNeedsIDs.Contains(ads.FK_NeedsList_ID))
.Select(an => an.FK_NeedsList_ID)
.Distinct()
.Count() == additionalNeedsIDs.Count
)

Should I keep adding conditions to my LINQ query or add a condition upfront and use two different queries?

I have the following LINQ Query:
var contents = _contentsRepository.GetAll()
.Where(a => a.SubjectId == subjectId &&
a.ContentTypeId == contentTypeId &&
a.ContentStatusId == contentStatusId )
.ToList();
I would like this select to proceed normally unless the contentStatusId == 99. If that's
the case then I want it to retrieve a row from the database with ANY contentStatusId.
Would it be best to do a check of contentStatusId first and then break this down into
two LINQ selects or is there a way I could modify my LINQ query?
Note that I am using SQL Server 2012 and my repository:
public virtual IQueryable GetAll() { return DbSet; }
I believe you can modify your query by adding a contentStatusId == 99 component to your predicate that will short-circuit the evaluation of a.ContentStatusId == contentStatusId like so:
var contents = _contentsRepository.GetAll()
.Where(a => a.SubjectId == subjectId &&
a.ContentTypeId == contentTypeId &&
(contentStatusId == 99 ||
a.ContentStatusId == contentStatusId))
.ToList();
In the normal case everything will work just like before.
In the case when contentStatusId equals 99, there will be overhead of evaluating contentStatusId == 99 for every row, although I think depending on repository you're querying this part could be inlined as a true. You should see for yourself how this impacts performance in your setup.
Try this
var contents = _contentsRepository.GetAll()
.Where(a => contentStatusId == 99 ? (a.SubjectId == subjectId &&
a.ContentTypeId == contentTypeId &&
a.ContentStatusId == contentStatusId) : (a.SubjectId == subjectId &&
a.ContentTypeId == contentTypeId) )
.ToList();
var contents = _contentsRepository.GetAll()
.Where(a =>
{
return a.ContentTypeId == 99 ||
(a.SubjectId == subjectId &&
a.ContentTypeId == contentTypeId &&
a.ContentStatusId == contentStatusId)
}

Categories

Resources