Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ Expresson Lambda , +
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
 
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.
 
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:
 
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 25-Aug-13 21:42pm
Edited 25-Aug-13 22:27pm
(no name)29.9K
v3

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



Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 26 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100