[英]subqueries in Linq to Sql
IM stuck in linq query where im searching database & showing result using 即时消息卡在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();
i can not display category name from category table where categoryID matches. 我无法从categoryID匹配的类别表中显示类别名称。
I do this in sql like this 我这样在SQL中这样做
how to do this in linq 如何在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
help me 帮我
If you are missing some mapping, why not use a simple join? 如果缺少某些映射,为什么不使用简单的联接?
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
};
or with proper mappings: 或具有适当的映射:
var q = from p in db.Products
where ...
select new
{
p.ProductCode,
...,
p.Category.Category
};
You can put the subquery into the Linq LET clause. 您可以将子查询放入Linq LET子句中。 See
看到
for example. 例如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.