我正在研究現有的解決方案。當我有兩個實體,如如何獲得在LINQ中使用連接的項目列表?
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime? DOB { get; set; }
private List<long> _accounts = new List<long>();
[Display(Name = "Account No")]
public List<long> Accounts
{
get { return _accounts; }
set { _accounts = value; }
}
[Display(Name = "Account No")]
public string AccountId
{
get { return string.Join(",", _accounts); }
set { _accounts = value != null ? value.Split(',').Where(s=>!string.IsNullOrWhiteSpace(s)).Select(s => Convert.ToInt64(s.Trim())).ToList() : new List<long>(); }
}
}
public class Account
{
public long Id { get; set; }
public string AccountName { get; set; }
public string AccountNo { get; set; }
}
,也有一個ViewModel
public class UserAccountViewModel
{
public long AccountId { get; set; }
public string AccountNo { get; set; }
public int UserId { get; set; }
public string UserName { get; set; }
}
現在,我怎樣才能從這些實體List<UserAccountViewModel>
?
你不能。你需要在AccountId和UserId之間有一個映射。你可以有5個用戶和10個賬戶,你不知道哪個用戶擁有每個賬戶。 – jdweng
有幾個問題。你爲什麼不把賬戶實體映射到用戶實體?其次,如果用戶可以擁有多個帳戶,爲什麼UserAccountViewModel不包含帳戶列表? – Saket
你需要在'帳戶'實體和'用戶'實體之間有關係 - 它似乎需要'ICollection'在它們之間進行映射。此外,它可能需要'UserAccountViewModel'中的'List Accounts'來存放多個帳戶。 –