Click here to Skip to main content
15,885,815 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have some issues with Linq lambda expression.
I want to filter some User's data with it's related nested list objects.

Current Code

C#
var aUser = (from u in db.Users
                             join c in db.UserContacts on u.UserId equals c.UserId
                             join a in db.AccountTypes on u.AccountTypeId equals a.AccountTypeId
                             join r in db.Roles on u.RoleId equals r.RoleId
                             where c.ContactTypeId == (int)Global.enContactType.Email 
                             select new { u.FirstName, u.LastName, u.DisplayName, u.Username, u.Status, u.UserId, Email = c.Description, AccountTypeName = a.Description, Role = r.Description });

if (strField == "Role")
    aUser = aUser.Where(x => x.Role.Contains(strName));
else if (strField == "DisplayName")
    aUser = aUser.Where(x => x.DisplayName.Contains(strName));
else if (strField == "AccountTypeName")
    aUser = aUser.Where(x => x.AccountTypeName.Contains(strName));
else if (strField == "Username")
    aUser = aUser.Where(x => x.Username.Contains(strName));
else if (strField == "Email")
    aUser = aUser.Where(x => x.Email.Contains(strName));
else if (strField == "Status")
{
    if ("true,1".Contains(strName.ToLower()))
        aUser = aUser.Where(x => x.Status == true);
    if ("false,0".Contains(strName.ToLower()))
        aUser = aUser.Where(x => x.Status == false);
}


But I want to do with Expression.

C#
System.Linq.Expressions.Expression<Func<UserContact, bool>> objEmail = c => c.ContactTypeId == 1;
                System.Linq.Expressions.Expression<Func<User, bool>> objTest = p => p.UserContacts.Where(objEmail).ToList();



                if (strField == "Role")
                    objTest = p => p.Role.Description.Contains(strName);
                else if (strField == "DisplayName")
                    objTest = p => p.DisplayName.Contains(strName);
                else if (strField == "AccountTypeName")
                    objTest = p => p.AccountType.Description.Contains(strName);
                else if (strField == "Username")
                    objTest = p => p.Username.Contains(strName);
                else if (strField == "Email")
                    objTest = p => p.UserContacts.SingleOrDefault().Description.Contains(strName);

                IQueryable<User> iUser =(IQueryable<User>) db.Users.ToList();

                iUser = iUser.Where(objTest);


Below is my User Model Class:

C#
public virtual AccountType AccountType { get; set; }
public virtual Currency Currency { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<UserAddress> UserAddresses { get; set; }
public virtual ICollection<UserContact> UserContacts { get; set; }
public virtual ICollection<UserImage> UserImages { get; set; }
public virtual ICollection<UserMessage> UserMessages { get; set; }
public virtual ICollection<UserMessage> UserMessages1 { get; set; }
public virtual ICollection<CourerCompanyTrailer> CourerCompanyTrailers { get; set; }
public virtual ICollection<CourerCompanyTrailer> CourerCompanyTrailers1 { get; set; }
public virtual ICollection<CourierCompanyServiceCategory> CourierCompanyServiceCategories { get; set; }
public virtual ICollection<Vehicle> Vehicles { get; set; }


I want to filter UserContact with it's ContactTypeId = 1

Thanks for advance.
Posted
Updated 25-Aug-13 22:27pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900