2017-05-22 49 views
0

我正在研究现有的解决方案。当我有两个实体,如如何获得在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>

+2

你不能。你需要在AccountId和UserId之间有一个映射。你可以有5个用户和10个账户,你不知道哪个用户拥有每个账户。 – jdweng

+0

有几个问题。你为什么不把账户实体映射到用户实体?其次,如果用户可以拥有多个帐户,为什么UserAccountViewModel不包含帐户列表? – Saket

+0

你需要在'帐户'实体和'用户'实体之间有关系 - 它似乎需要'ICollection'在它们之间进行映射。此外,它可能需要'UserAccountViewModel'中的'List Accounts'来存放多个帐户。 –

回答

0

最后我得到我的回答。就像下面的例子:

 var users = new List<User>{ 
     new User{Id=1 ,Name="Test1",Email="[email protected]",DOB=DateTime.Now,AccountId ="1"}, 
     new User{Id=2 ,Name="Test2",Email="[email protected]",DOB=DateTime.Now,AccountId ="2"}, 
     new User{Id=3 ,Name="Test3",Email="[email protected]",DOB=DateTime.Now,AccountId ="3,4"} 
     }; 
     var accunts = new List<Account> 
     { 
      new Account {Id=1,AccountName = "A",AccountNo = "1"}, 
      new Account {Id=2,AccountName = "B",AccountNo = "2"}, 
      new Account {Id=3,AccountName = "C",AccountNo = "3"}, 
      new Account {Id=4,AccountName = "D",AccountNo = "4"}, 
     }; 

     var userAccounts = (from u in users 
        from a in accunts 
        where u.Accounts.Contains(a.Id) 
        select new UserAccountViewModel 
        { 
         AccountNo = a.AccountNo, 
         AccountId = a.Id, 
         UserId = u.Id, 
         UserName = u.Name 
        }).ToList(); 
0
var UserAccountViewModel = (from Account in list1 
      join UserAccountViewModel in list2 
      on Account .AccountNo equals UserAccountViewModel.AccountNo 
      select new { UserAccountViewModel }).ToList(); 

希望它可以帮助你。 或提高性能按照建议

var query = dataContext.UserAccountViewModel 
      .Include(o => o.Account) 
      .Where(t=> t.AccountNo == t.Account.AccountNo).ToList(); 
+4

在使用实体框架时,使用导航属性优于连接。这是更好的优化和更好的理解。 – Mafii

+0

这不是答案。请先阅读并理解我的问题。我需要的是我所要求的。 – Ashiquzzaman

0

如果你有accnouts以及您要创建列表中的用户的两个列表,你可以使用:

var userAccount = (from account in list1 
     join user in list2 
     on account.Id.ToString() equals user.AccountIds 
     select new UserAccountViewModel 
     { 
     AccountId = account.Id, 
     AccountNo = account.AccountNo, 
     UserName = user.Name, 
     UserId = user.Id 
     }).ToList(); 
+0

你在BD?好像我可以来到你的办公室并帮助你。 :) –

+0

这不是答案。请先阅读并理解我的问题。我需要正是我所要求的。从我的个人资料中找到我的公司名称,并且您随时欢迎。 – Ashiquzzaman

+0

请您详细解释一下这个问题,请先帮助我们理解问题。 –

相关问题