[RESOLVED]Linq to Sql question

var query =
from
c
in
db.Customers

where
c.CustomerId == customerId

select
c.Comments;

ViewBag.Comments = query;

I’m trying to retrieve the comments against the Customer.

It should only retrieve one record, since I filter it by customerId.

How do I get the data and assign to the Viewbag?

Since query needs to be executed before it returns the data.

alphabeatsco

How do I get the data and assign to the Viewbag?

Since query needs to be executed before it returns the data.

ViewBag.Comments =query.ToList();

var query = db.Customers.Where(c=>c.CustomerId==customerId).Select(c=>c.Comments).Single();

 

Use FirstOrDefault

ViewBag.Comments = query.FirstOrDefault();

You can see another solution at here :

http://youtu.be/X4VRBJP7ezQ

Leave a Reply