Dzendo
Dzendo

Reputation: 199

Linq - drill down with find

I am completely new to linq and need help. These are my poco classes:

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

I would like to search for valid Users with special customers. Special customers would come from another user. So I would send another user as method argument.

Is it even possible with linq or I need stored procedure to solv the problem?

Best regards

Upvotes: 2

Views: 303

Answers (2)

Somnath
Somnath

Reputation: 3277

Try this..

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

Upvotes: 0

mipe34
mipe34

Reputation: 5666

You can try this:

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

But beware, it will work only if your relations are populated (included) like in my example here.

NOTE

You should not call virtual member inside the constructor. Anwer is here on SO: Virtual member call in a constructor

Upvotes: 1

Related Questions