EF Lambda 多表查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcForLamadaToTableJion.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
LamadaJoinEvent();

 


return View();
}

/// <summary>
/// 实体类
/// </summary>
public class Product
{

public int ProductId { get; set; }

public string ProductName { get; set; }
}

#region 填充 数据
static Product[] product2 =
{
new Product{ProductId=1,ProductName="Apple"},
new Product{ProductId=4,ProductName="Orange"},
new Product{ProductId=5,ProductName="Berry"},
};
static Product[] product3 =
{

new Product{ProductId=4,ProductName="Orange"},
new Product{ProductId=5,ProductName="Berry"},
};
#endregion

#region 调用lamada多表查询方法
/// <summary>
/// 调用lamada多表查询方法,
/// 说明:这种方法目前比较适合两张表连接,三张表以上估计够呛
/// </summary>
public void LamadaJoinEvent()
{


var result3 = product2.Join
(
product3,
x => x.ProductId,
c => c.ProductId,
(x, c) => new { x.ProductId, c.ProductName }
);

foreach (var a in result3)
{
ViewBag.data +="||" +a.ProductName;
}


}
#endregion

public ActionResult About()
{
ViewBag.Message = "你的应用程序说明页。";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "你的联系方式页。";

return View();
}

 

}
}

你可能感兴趣的:(EF Lambda 多表查询)