第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用嵌套 SQL 的視圖模型中的 LINQ 數(shù)據(jù)

使用嵌套 SQL 的視圖模型中的 LINQ 數(shù)據(jù)

C#
桃花長相依 2021-10-09 16:31:14
我可以使用以下 TSQL 訪問數(shù)據(jù):select Sweets.*, Qtyfrom Sweetsleft join (select SweetID,  Qty from carts where CartID = '7125794e-38f4-4ec3-b016-cd8393346669' ) t   on Sweets.SweetID = t.SweetID但我不確定如何在我的 Web 應用程序上實現(xiàn)相同的結果。有誰知道如何使用 LINQ 實現(xiàn)這一目標?到目前為止,我有: var viewModel = new SweetViewModel {   Sweet = db.Sweets.Where(s => db.Carts.Any(c => c.SweetID == s.SweetID)) };編輯:對不起,我應該指定我正在使用 2 個類的視圖模型:查看型號:public class SweetViewModel{    public IEnumerable<Sweet> Sweet { get; set; }    public IEnumerable<Cart> Cart { get; set; }    //public Cart OrderQty { get; set; }}public class Sweet{    public int SweetID { get; set; }    public int CategoryID { get; set; }    public Category Category { get; set; }    public string SweetName { get; set; }    public bool Singular { get; set; }    public string Description { get; set; }    public decimal Price { get; set; }    public virtual ICollection<Cart> Carts { get; set; }}public class Cart{    [Key]    public int RecordID { get; set; }    public string CartID { get; set; }    public int SweetID { get; set; }    public int PemixID { get; set; }    public int Qty { get; set; }    public System.DateTime DateCreated { get; set; }    public Sweet Sweet { get; set; }    public PreMix PreMix { get; set; }}
查看完整描述

3 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

以下將起作用


from sweet in db.Sweets

join cart in db.Carts 

on sweet.SweetID equals cart.SweetID into swct

from sc in swct.DefaultIfEmpty()

select new { Sweet = sweet, Qty = sweet.Key == sc.Key ? sc.Qty : 0 }


查看完整回答
反對 回復 2021-10-09
?
哈士奇WWW

TA貢獻1799條經驗 獲得超6個贊

您應該使用 LINQ 連接函數(shù)。


對于我的示例,我還使用了您的 SQL 查詢的更改版本,我認為它是相同的:


SELECT sweets.*, carts.Qty

FROM sweets LEFT JOIN carts ON sweets.SweetID = carts.SweetID

WHERE carts.CartID = '7125794e-38f4-4ec3-b016-cd8393346669'

然后我使用 JOIN 函數(shù)將這個新查詢轉換為 LINQ。


var cartId = '7125794e-38f4-4ec3-b016-cd8393346669'

var query = db.Sweets    // table in the "from" statement

   .GroupJoin(db.Carts, // all carts for that sweet will be joined into [sweets -> Cart[]]

      cart => cart.SweetID,        // the first part of the "on" clause in an sql "join" statement

      sweet => sweet.SweetID,   // the second part of the "on" clause)

      (sweet, carts) => new { Sweet = sweet, Carts = cart }) // create new compound object

   .SelectMany(

              sweetsCarts => sweetsCart.Carts.DefaultIfEmpty(), //show the sweet even if there is no cart

              (x,y) => new { Sweet = x.Sweet, Cart = y });

   .Where(sweetsCart => sweetsCart.Cart.CartID == cartId);    // restrict your cartID

基本上,該Join函數(shù)生成一個復合對象列表,其中包含一個Sweet對象和一個Cart帶有每個列表條目的對象,因此您可以訪問sweetsCart.Cart.CartID或sweetsCart.Sweets.SweetID。


=>順便說一下,左側的名稱可以是您想要的任何名稱,它只是 LINQ 的標識符。我選擇稱它為“sweetsCart”。


帶有 SelectMany 的 GroupJoin 可以進行左連接。


查看完整回答
反對 回復 2021-10-09
  • 3 回答
  • 0 關注
  • 200 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號