Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI All,

I am new to Linq so I request for your help me in this issue.

so I have methods which return a list of the user as shown below

var usernames = getuser(userID.ToString());


getuser method as show below

public static List<string> GetUser(string id)
       {

           List<string> user1 = new List<string>();
           if (!string.IsNullOrWhiteSpace(id))
           {
               getUser repository = new getUser();
               user1 = repository.GetUserbyid(id);
               repository.Dispose();
           }
           return user1;
       }


so I am getting all the users using the string builder

foreach (var item in usernames)
   { sb.AppendLine("p.username.Contains("+ item.ToString().ToLower()+")" + "||"); }


the output of sb as shown below

{p.CreatedBy.Contains(011)||p.CreatedBy.Contains(009)||p.CreatedBy.Contains(0000000010)}


so I have a predicate as shown below

var predictate = PredicateBuilder.True<Getuserdeatils>();


so I have the query where it used for the different operation.so I have to changed only the predictate part of the query as shown below.

var query = (from sr in context.Getuser.AsExpandable().Where(predictate)
                             join wi in context.UserProfiles on sr.CreatedBy equals wi.UserName into list1
                             from l1 in list1.DefaultIfEmpty()



so in the above query i want predictate as following

predictate = predictate.And( p => p.username.Contains("010") ||p.username.Contains("011") || p.username.Contains("009"));


thanks in advance.

What I have tried:

I have tried as follwoing

foreach (var item in usernames)
               {
        predictate = predictate.And(p =>p.CreatedBy.Contains(item.ToString()));

               }



and

predictate = predictate.And(p =>((bool.Parse(sb.ToString()))));
Posted
Updated 31-Jul-18 2:08am
Comments
F-ES Sitecore 30-Jul-18 6:56am    
Forget trying to do it this way. Build your predicate as you go rather than building a string in StringBuilder.

predicate = predicate.And(someCondition)

1 solution

You don't need to loop for usernames. You can do it in a single phrase:
predicate = predicate.And(p => usernames.Contains(p.CreatedBy));
 
Share this answer
 
Comments
manoj1412012 1-Aug-18 1:33am    
thanks, Ahmed Raouf

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