简体   繁体   中英

LINQ query of List of table object returning different results than LINQ-to-SQL query for the same data

In my ASP.NET MVC 5 project I'd like to take a LINQ-to-SQL query result and persist it in memory to do further queries on rather than hit the database each time. However when those results are put in a List and then further where clauses are applied I end up with no results as where the same where clauses applied to a query directly from the database does return results.

This returns items in my list:

DataContext db = new DataContext();
var data = db.myView.Where(x => [where clauses]).ToList();

This doesn't return items in my list:

DataContext db = new DataContext();
var data1 = db.myView.ToList();
var data2 = data1.Where(x => [where clauses]).ToList();

I've used this method to query from memory rather than directly from the database plenty of times and have never seen a difference in my results.

Here is the actual where logic:

.Where(x => ((((DateTime)x.DINVPDOF).Year == year && ((DateTime)x.DINVPDOF).Month == month && x.SOPNUMBE.Trim().ToUpper().StartsWith("SINV")) || ((x.DOCDATE >= new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddMonths(-1) && x.DOCDATE <= new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1).AddDays(-1)) && (x.SOPNUMBE.Trim().ToUpper().StartsWith("CR") || x.SOPNUMBE.Trim().ToUpper().StartsWith("RC")))) && (x.USCATVLS_6 == "FGS") && (x.QUANTITY == 1) && !x.CUSTCLAS.Contains("SER") && (x.SLPRSNID != "HOUSE") && !(x.ITEMDESC.Contains("RETURN") && !x.ITEMDESC.Contains("CREDIT")))

Does it make sense that these two methods should ever not return the same thing? Maybe something in the where clauses that is giving me behavior different than I'm used to?

Thanks.

Looks like some of the database fields had spaces after the values. LINQ threw out records due to no string equivalency however apparently LINQ-to-SQL ignored the padding when applying the where clauses.

Trimming the fields in the LINQ query yields the same results now as the LINQ-to-SQL query.

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