Reputation: 2473
I have a search method below using linq, my search citeria is based on one table so far but I have a particular citeria which requires another table :
Method:
public void search(string s)
{
var db = new CommerceEntities();
var products =
from p in db.Products
where (p.ModelName != null && p.ModelName.Contains(s))
|| SqlFunctions.StringConvert((double) p.ProductID).Contains(s)
|| (p.ModelNumber != null && p.ModelNumber.Contains(s))
|| (p.Description != null && p.Description.Contains(s))
|| SqlFunctions.StringConvert((double) p.CategoryID).Contains(s)
|| //stuck - See comment below
/* so far all in 'products' table, but I have one more citeria here that needs 'categories' table.
Ok, the query in SQL statement will be like this:
select ProductID, ModelName, ProductImage, Unitcost, products.Categoryid
from categories
join products
on (categories.CategoryID = products.categoryID)
where categories.categoryname = 'necklace' (replace necklace with my parameter 's')
order by products.ModelName
I am not sure how to 'integrate' it with my existing linq query. Please kindly advice. Thanks.
*/
select new
{
// Display the items
p.ProductID,
p.ModelName,
p.ProductImage,
p.UnitCost,
p.CategoryID,
};
ListView_Products.DataSourceID = null;
ListView_Products.DataSource = products;
}
Upvotes: 0
Views: 74
Reputation: 397
var products = from p in db.Products
join c in categories on c.CategoryID equals p.categoryID
where (/* all you conditions in either tables */)
select new
{
p.ProductID,
p.ModelName,
p.ProductImage,
p.UnitCost,
p.CategoryID
/* plus what ever you want from categories */
};
Upvotes: 1