简体   繁体   English

如何在LINQ中使用join获取项目列表?

[英]How to get List of item using join in LINQ?

I am working on an existing solution. 我正在研究现有的解决方案。 Where I have two entities like 我有两个实体

  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; }

    }

and also have one ViewModel 还有一个ViewModel

  public class UserAccountViewModel
    {
        public long AccountId { get; set; }
        public string AccountNo { get; set; }            
        public int UserId { get; set; }
        public string UserName { get; set; }

    }

Now, how can I get List<UserAccountViewModel> from those entities? 现在,如何从这些实体中获取List<UserAccountViewModel>

var UserAccountViewModel = (from Account in list1
             join UserAccountViewModel in list2
             on Account .AccountNo equals UserAccountViewModel.AccountNo 
             select new { UserAccountViewModel }).ToList();

Hope it helps you. 希望对您有帮助。 Or to increase the performance as per suggestion 或根据建议提高性能

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

If you have two lists of accnouts and users from which you want to create List, you can use: 如果您有两个要创建列表的帐户和用户列表,则可以使用:

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();

At last I get my answer.It's something like below example: 最后我得到了答案,就像下面的例子:

        var users = new List<User>{
        new User{Id=1 ,Name="Test1",Email="test1@ds.com",DOB=DateTime.Now,AccountId ="1"},
        new User{Id=2 ,Name="Test2",Email="test2@ds.com",DOB=DateTime.Now,AccountId ="2"},
        new User{Id=3 ,Name="Test3",Email="test3@ds.com",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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM