我坚持使用linq查询我搜索数据库&使用
显示结果DBSearchDataContext db = new DBSearchDataContext();
object q = from b in db.Products
where b.ProductCode.Contains(val) |
b.ProductName.Contains(val) |
b.Specification.Contains(val) |
b.Description.Contains(val) |
b.Category.Contains(val)
select b;
GridView1.DataSource = q;
GridView1.DataBind();
我无法在categoryID匹配的类别表中显示类别名称。
我在像这样的SQL中执行此操作
如何在linq中执行此操作
SELECT ID, ProductCode, DisplayOrder, ProductName, imgThumb, inStock, Status, Amount, (SELECT Category FROM Category AS aaa WHERE (Products.CategoryID = CategoryID)) AS Category FROM Products ORDER BY ID DESC
帮帮我
答案 0 :(得分:1)
如果您缺少某些映射,为什么不使用简单的连接?
var q = from p in db.Products
join c in db.Category on p.CategoryID equals c.CategoryID
where ...
select new
{
p.ProductCode,
...,
c.Category
};
或使用适当的映射:
var q = from p in db.Products
where ...
select new
{
p.ProductCode,
...,
p.Category.Category
};
答案 1 :(得分:0)