0

I am having a User Table like this,

Id(int)
Name(string),
State_Id(Int64),
Country_Id(Int64),
MobileNumber(String),
email(String),
identity(String)
Role_Id
Disable(Boolean)

Identity column will have values comma separated for example(,mole in hand, mole in face,). this value are Pre-Defined,

Now I have a request like this,

class UserRequest
{
public String RoleName{get;set;}
public String CountryName{get;set;}
publi String StateName{get;set;}
publi IList<String> identityLst{get;set;}
}

Using the request i want to form a Query.

public IList<User> GetUser(UserRequest req)
{

 IQueryable<User> query=dataContext.user.where(a=>!a.isdisable)
 if(String.IsNullOrEmpty(req.RoleName))
 {
  query=from qu in query
      from role in datacontext.role.where(a=>a.id==qu.role.id)
      where role.name==req.RoleName
      select qu;
}

if(String.IsNullOrEmpty(req.CountryName))
{
query=from qu in query
      from country in datacontext.country.where(a=>a.id==qu.country.id)
      where country.name==req.CountryName
      select qu;
}

if(String.IsNullOrEmpty(req.StateName))
{
query=from qu in query
      from state in datacontext.state.where(a=>a.id==qu.state.id)
      where state.name==req.StateName
      select qu;
}

if(req.identityLst!=null)
{
/////Here I need a query with or condition, Is it possible. 
/////for example if identity List have 3 value. i need a 3 or condition 
}
}

please help me out of this problem, If identityLst have 10 value I am hitting DB 10 times.

1 Answers1

-1

Don't do it as qry on the server but do some post filtering at runtime.

public IList<User> GetUser(UserRequest req){
    //remove the [if(req.identityLst!=null)] part
}

public bool ContainsIdentity(IList<string> x, IList<string> y)
{
    if( x == null || y == null ) return false;
    return x.Any(test => y.Contains(test));
}

var users = GetUser(req).ToList();
var filtered = users.Where( u => ContainsIdentity(u.identity.Split(','), req.identityLst ));
lboshuizen
  • 2,746
  • 17
  • 20