嘗試使用 EF 和 LINQ 連接 3 個 SQL 表,但不確定如何撰寫正確的腳本。我知道如何用兩張桌子來做這件事,但是一旦第三張桌子上場,我就有點迷失了。
我有 3 張桌子,模型看起來像這樣:
用戶:
public class User
{
public int Id { get; set; }
public DateTime Created { get; set; }
}
用戶名:
public class UserName
{
public int Id { get; set; }
public int UserId { get; set; }
public string Name { get; set; } = null!;
public User User { get; set; } = null!;
}
用戶名值:
public class UserNameValues
{
public int Id { get; set; }
public int UserNameId { get; set; }
public string Value { get; set; } = null!;
public UserName UserName { get; set; } = null!;
}
我想從中獲得價值UserNameValues
where user.id = x and username.name = x
我很感激任何幫助!
uj5u.com熱心網友回復:
試試這個(假設一個context用 集合呼叫的變數UserNameValues):
context.UserNameValues.Where(unv => unv.UserName.Name == "juan" && unv.UserName.User.Id == x);
我個人不喜歡類似 SQL 的語法,很久以前就停止使用它來支持方法呼叫,正是因為它令人困惑。
uj5u.com熱心網友回復:
請參考以下鏈接。我被困在如何撰寫 LINQ 來連接三個不同的表,如下所示:
https://social.msdn.microsoft.com/Forums/en-US/21444ee2-0f39-4f6b-9b6b-50977e629136/join-three-tables-using-linq-query?forum=aspadoentitylinq
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/527942.html
標籤:网实体框架林克
上一篇:演算法題--用兩個堆疊實作佇列
