【问题标题】:How to get List of item using join in LINQ?如何在 LINQ 中使用 join 获取项目列表?
【发布时间】:2017-05-22 09:18:18
【问题描述】:

我正在研究现有的解决方案。我有两个实体,比如

  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&lt;UserAccountViewModel&gt;?

【问题讨论】:

  • 你不能。您需要在 AccountId 和 UserId 之间进行映射。您可能有 5 个用户和 10 个帐户,但您不知道哪个用户拥有每个帐户。
  • 几个问题。为什么不将 Account 实体映射到 User 实体?其次,如果一个用户可以有多个帐户,为什么 UserAccountViewModel 不包含帐户列表?
  • 您需要在Account 实体和User 实体之间建立关系-似乎需要ICollection 在它们之间进行映射。此外,它可能需要List&lt;Account&gt; Accounts 中的UserAccountViewModel 来保存帐户数量。
  • 我需要按账户列出用户。而我不能这样做。因为我正在研究现有的解决方案。
  • 您似乎误用了单数和复数,这会造成混淆,并且总体上不利于示例和代码库的可读性。 _account(单数)是一个列表(多个对象); AccountIds(复数)返回一个字符串(单数)。

标签: c# .net asp.net-mvc entity-framework linq


【解决方案1】:
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();

【讨论】:

  • 在使用实体框架时,使用导航属性优于连接。它得到了更好的优化和更好的理解。
  • 这不是答案。请先阅读并理解我的问题。我需要的正是我所要求的。
【解决方案2】:

如果您有两个要从中创建列表的帐户和用户列表,您可以使用:

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

【讨论】:

  • 你在BD?看来我本可以到你的办公室帮助你。 :)
  • 这不是答案。请先阅读并理解我的问题。我需要的正是我所要求的。从我的个人资料中找到我的公司名称,我们随时欢迎您。
  • 能否请您再详细说明一下这个问题,以帮助我们先理解这个问题。
【解决方案3】:

我终于得到了答案。就像下面的例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    相关资源
    最近更新 更多