【问题标题】:Linq - drill down with findLinq - 使用查找向下钻取
【发布时间】:2013-01-17 13:25:08
【问题描述】:

我对 linq 完全陌生,需要帮助。 这些是我的 poco 课程:

public class User {
   public User()
   {
      this.Profiles = new List<Profile>();
   }

   public Guid ID { get; set; }        
   public bool IsValid{ get; set; }       
   public virtual ICollection<Profile> Profiles { get; set; }
}

public class Profile {
   public Profile() {
      this.Users = new List<User>();
      this.Customers = new List<Customer>();
   }

   public Guid ID { get; set; }
   public string Name { get; set; } 
   public virtual ICollection<User> Users { get; set; }
   public virtual ICollection<Customer> Customers { get; set; }
}

public class Customer {
   public Customer()
   {
      this.Profiles = new List<Profile>();
   }

 public Guid ID { get; set; }
 public string Number { get; set; }       
 public virtual ICollection<Profile> Profiles { get; set; }
}

我想搜索有特殊客户的有效用户。特殊客户将来自另一个用户。所以我会发送另一个用户作为方法参数。

是否可以使用 linq 或者我需要存储过程来解决问题?

最好的问候

【问题讨论】:

  • 请尝试更好地描述您的问题。你能发布例如您正在尝试编写的方法的一些伪代码(可能的输入和预期的输出)?
  • @mipe34 嗨,列表 user = FindAllUsersBySameCustomers(User sourceuser);我想要一个具有与方法中提供的相同客户的用户列表。希望对你有帮助

标签: linq join lambda


【解决方案1】:

你可以试试这个:

public static List<User> FindAllUsersBySameCustomers(User sourceuser)
{
    var res = sourceuser.Profiles.SelectMany(p => p.Customers)
                                 .SelectMany(c => c.Profiles)
                                 .SelectMany(p => p.Users)
                                 .Distinct();
    return res.ToList();
}

但请注意,只有在我的示例 here 中填充(包含)您的关系时,它才会起作用。

注意

你不应该在构造函数中调用虚拟成员。 Anwer 在这里:Virtual member call in a constructor

【讨论】:

    【解决方案2】:

    试试这个..

    /// <summary>
        /// Search for valid Users with special customers. 
        /// Special customers would come from another user. 
        /// So I would send another user as method argument.
        /// </summary>
        /// <returns></returns>
        public List<User> FindValidUsersWithSpecialCustomers(List<User> allUsers, User anotherUser)
        {   
            var specialCustomers = anotherUser.Profiles.SelectMany(aProfile => aProfile.Customers);//.Select(cust => cust.Number == "SpecialCustomerNumber" && cust.ID == new Guid("SpecialCustomerGuid"));
    
            Func<IEnumerable<Customer>, IEnumerable<Customer>, Boolean> IsSpecialCustomersPresentInThisCustomersList =
                delegate(IEnumerable<Customer> customerList, IEnumerable<Customer> specialCustomersList)
                {
                    if ((from cust in customerList where specialCustomersList.Contains(cust) select cust).Any())
                        return true;
                    else
                        return false;
                };
    
            var validUsersWithSpecialCustomers = (from user in allUsers where user.IsValid && IsSpecialCustomersPresentInThisCustomersList(user.Profiles.SelectMany(p => p.Customers), specialCustomers) select user);
    
            return validUsersWithSpecialCustomers.ToList();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多