2

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)

David
  • 208,112
  • 36
  • 198
  • 279
ColdFusion
  • 93
  • 8

3 Answers3

4

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

NetSide
  • 3,849
  • 8
  • 30
  • 41
0

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

Try adding the result to an empty list.

Andrew Hanlon
  • 7,271
  • 4
  • 33
  • 53
0

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

msarchet
  • 15,104
  • 2
  • 43
  • 66