-3

我正在编写一个 ASP.NET MVC 3 应用程序。我用实体框架和 linq 编写,但我对 linq 语句有疑问

  1. 如果你有:

    select a, (select d from tabled where id = '12 ') as c from tableA inner join TableB on tablea.id = tableb.id
    

    然后切换到linq how(使用控制器并分配ViewBag.str = ...)(视图如何,可以显示a和c)

  2. 如果他的陈述是,例如:Product product = db.Product.Find (id) 那么你如何确定产品是否为空?

4

2 回答 2

0

var a= from p in tableA join q in tableB on p.id 等于 q.id select new {pa, from r in tabled where r.id == 12 select rd}.ToList();

这是该语句的 LINQ 查询。还

您可以使用

Product product = db.Product.Find (id);
if (product == null) return "Emplty";
于 2013-03-01T04:52:22.997 回答
0

您必须遵循以下步骤;1)在模型中创建返回值类:

public class ClassResult
{
    public int a1 { get; set; }
    public int b1 { get; set; }
}

然后在控制器上

编写查询如下:

 public ActionResult Index()
    {
        List<TableAClass> ListA = new List<TableAClass>();
        List<TableBClass> ListB = new List<TableBClass>();
        List<TableCClass> ListC = new List<TableCClass>();

        List<ClassResult> res = new List<ClassResult>();

        res = (from p1 in ListA join p2 in ListB on p1.a1 equals p2.b1 select new ClassResult { a1=p1.a1 ,b1=p2.b1 }).ToList();

        return View(res);
    }

3) 在视图上,

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.ClassResult>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index1</title>
</head>
<body>
    <fieldset>
        <legend>Fields</legend>

        <div class="display-label">a1</div>
        <div class="display-field"><%: Model.a1 %></div>

        <div class="display-label">b1</div>
        <div class="display-field"><%: Model.b1 %></div>

    </fieldset>


</body>
</html>
于 2013-03-01T11:12:24.497 回答