-3

ASP.NETMVC3アプリケーションを作成しています。エンティティフレームワークと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

次の手順に従う必要があります。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 に答える
0

var a= tableA の p から p.id が q.id と等しい場合に tableB の q に参加する select new {pa, from 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 に答える