简体   繁体   中英

GridView doesn't show results with First() query extension

I am using LINQ-to-Entities and have loaded up a Northwind Database (with LazyLoading=true).

var db = new NorthwindEntities();
var result = db.Orders.Where(x => x.CustomerID == "ANATR").First();        
DataGridView1.DataSource = result;

The above code doesn't show up any items (one in this particular case) in the DataGridView. What am I doing wrong?

(If I remove the 'First()' it works fine and gives me several items)

The result is not a collection, that's why, you can not list it. First is a single object.

Your result is actually a single object not a collection of objects.

Try adding the result to an empty list.

var db = new NorthwindEntities(); var result = db.Orders.Where(x => x.CustomerID == "ANATR").First();
DataGridView1.DataSource = result;

since result isn't a Collection this won't work you can use

var results =Enumerable.Repeat(result, 1);

To create single item in a list to do this or also

var results = new List<Order>() { result };

Will also work

And then bind to results instead of result

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