Ef6 select returns nothing after just selecting FirstOrDefault ()

I have an EF6 data access context that seems to work. There is one that returns cannot evaluate the expression. This is FirstOrDefault. If I execute the query without FIrstOrDefault, IEnumerable contains 1 element that I am looking for. Where am I going wrong? I have many other FirstOrDefaults that work great.

var a = "";
var products = from p in _db.Products
               where p.SKU == isbn
               select p;

foreach (var productx in products)
{
    a = productx.SKU; <- productx contains the valid product
}

var product = (from p in _db.Products
               where p.SKU == isbn
               select p).FirstOrDefault(); <-- can't evaluate

The same thing happens even if I update the context ....

Both return the correct string in LinqPad ...

Products.FirstOrDefault(p=>p.SKU == "9781250033697")

(from p in Products where p.SKU == "9781250033697" select p).FirstOrDefault()

They give null in my application (isbn = "9781250033697")

var product = _db.Products.FirstOrDefault(p=>p.SKU == isbn);

            var products = from p in _db.Products
                where p.SKU == "9781250033697"
                select p;

Other Ling requests against the same db give valid data.

+4
source share
4 answers
var product = db.Products.FirstOrDefault(p=>p.SKU == isbn);
0
source

With LINQPad

/* all records */
var query = (from r in Regions select r);
query.Dump();

/* first record */
var query2 = (from r in Regions select r).FirstOrDefault();
query2.Dump();

/* "00006" record */
var query3 = (from r in Regions where r.MaestroId == "00006" select r).FirstOrDefault();
query3.Dump();

LINQPad , , , . Hardcode ISBN, , , , LINQ , , , ... EF6 ...

0

SQL-, framework 6

, , ;

:

using (var context = new BlogContext()) 
{ 
    context.Database.Log = Console.Write; 

    // Your code here... 
}
0

.

.

, . , .

0

Source: https://habr.com/ru/post/1534930/


All Articles