Linq to sql 实现多条件的动态查询(方法一)

/// <summary>

/// Linq to sql 多字段动态查询

/// </summary>

/// <returns></returns>

private List<TVacant> ViewBinding(ModelDataContext db,string fyno,string brd,string area,string city,string pos)

{

Expression<Func<TVacant, bool>> expr = n => GetCondition(n,fyno,brd,area,city,pos);

var xQuery = db.TVacant.Where<TVacant>(expr.Compile());

return xQuery.ToList<TVacant>();

}

private bool GetCondition(TVacant tb,string fyno,string brd,string area,string city,string pos)

{

bool boolResult = true;

if (!String.IsNullOrEmpty(fyno))

{

boolResult &= tb.fy_no == fyno;

}

if (!String.IsNullOrEmpty(brd))

{

boolResult &= tb.brd_no == brd;

}

if (!String.IsNullOrEmpty(area))

{

boolResult &= tb.area_no == area;

}

if (!String.IsNullOrEmpty(city))

{

boolResult &= tb.city_no == city;

}

if (!String.IsNullOrEmpty(pos))

{

boolResult &= tb.pos_no == pos;

}

return boolResult;

}

你可能感兴趣的:(LINQ)