jenifer
jenifer

Reputation: 1

How to write a join var datacontext?

DataContext db = new DataContext(conString);
var dvd = db.GetTable<DvdList>();
var category = db.GetTable<CategoryList>();
var query= from b in dvd 
           join category on dvd.CategoryId equals category.CategoryId  
           where b.Title.Contains(txtSearch.Text)
           select b;
GridView1.DataSource =query;

here has error "join category on dvd"

Upvotes: 0

Views: 1544

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503469

You're trying to use category as both the range variable name and the collection name. Try this:

var query= from b in dvd 
           join c in category on dvd.CategoryId equals c.CategoryId  
           where b.Title.Contains(txtSearch.Text)
           select b;

(As noted in comments, the join is really just going to filter out DVDs whose category ID isn't in the category table... in your real query, are you actually using the category?)

Upvotes: 3

Related Questions