简体   繁体   中英

Linq to sql query not returning correct items

Hi i am having an issue with a query to my db. I have looked through the questions on here and have not found anything similar although my title is similar to some other questions. The site i am working on can create collection requests and then view them in a table to manage existing requests. The first query i used works and returns the correct requests. The original query can be seen below.

var query = context.LogisticsRequests.AsQueryable().Where(x => x.Type == types.Collection.ToString());

A new option to delete a request has been added however this is just a new column in the db called isDeleted, this way the request is removed from the table but there is still an archived copy. So in order to filter out deleted requests i updated the query as seen below.

var query = context.LogisticsRequests.AsQueryable().Where(x => x.Type == types.Collection.ToString() && x.isDeleted != true);

After changing the query no results are returned and i cant understand why, only one request is set to deleted and most of the others will have a null value so they should still be returned since there not equal to true. Ive included some screenshots below showing the query result and the query sent to the database. Thank you for any help or suggestions.

在此处输入图片说明

The query sent to the db:

    {SELECT [t0].[LogisticsRequestId], [t0].[CreatedOn], [t0].[ModifiedOn], [t0].[CreatedById], [t0].[ModifiedById], [t0].[Type], [t0].[ClientId], [t0].[SupplierReference], [t0].[DateRequested], [t0].[CourierType], [t0].[ClientName], [t0].[Laboratory], [t0].[LaboratoryId], [t0].[Reference], [t0].[SpecialInstructions], [t0].[ContactName], [t0].[ContactNo], [t0].[ContactEmail], [t0].[CollectionAddress], [t0].[UserLogisticsAddressId], [t0].[ClientLogisticsAddressId], [t0].[SiteName], [t0].[CollectionInstructions], [t0].[RequestDetails], [t0].[EstimatedDeliveryDate], [t0].[Status], [t0].[Comment], [t0].[NumberCoolBoxes], [t0].[CollectionFrom], [t0].[CollectionTo], [t0].[CallBefore], [t0].[PleaseCall], [t0].[isDeleted]
FROM [dbo].[LogisticsRequest] AS [t0]
WHERE ([t0].[Type] = @p0) AND (NOT ([t0].[isDeleted] = 1))
}

Again thank you for any help.

Make sure that in IsDelete column there must be value. For all record it must be either true or false . Make sure you are not storing null in IsDelete column. If it's null then you have to set true or false .

Change your query to

var query = context.LogisticsRequests.AsQueryable().Where(x => x.Type == types.Collection.ToString() && !(x.isDeleted == null ? false : x.IsDeleted));

You should check if the new column "isDeleted" is filled for all rows. If the column is newly created, it's possible that it contains null , so the SQL query will fail. Fill the table with true and false .

Also, as mentioned in the comments, you should rather use "!isDeleted".

Curious about this

Where(x => x.Type == types.Collection.ToString()

is types a collection of types? Then it should read

 types.Collection.Contains(x.Type)

Like so

Where(x => types.Collection.Contains(x.Type) && !x.isDeleted)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM