linq to sql 模糊查询
linq 模糊查询,查询的字段名不定,字段名我在下拉列表中选择,我选择哪个就对哪个字段进行模糊查询,
这样的查询语句怎么写啊
[解决办法]
模糊查询就要做的事比较多了。你看一下我写的linq 动态查询吧
http://user.qzone.qq.com/452591170/infocenter
[解决办法]
方法很多:
1.使用System.Linq.Dynamic
2.披着Linq的外衣拼接SQL语句
http://www.cnblogs.com/snowdream/archive/2008/07/18/1246308.html
[解决办法]
//以Norhwind数据库为例
//假如查询CustomerID
Query(o => o.CustomerID.Contains("aaaa"));
//假如查询ShipAddress
Query(o => o.ShipAddress.Contains("aaaa"));
Console.ReadKey();
}
static List<l8r.Orders> Query(Func<l8r.Orders, bool> predicate)
{
return DbContent.Orders.Where(predicate).Select(o => o).ToList();
}
using (NorthwindDBDataContext northwind = new NorthwindDBDataContext(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
{
var productsInfo = from p in northwind.Products select p;
if (!string.IsNullOrEmpty(txtProductName.Text))
productsInfo = productsInfo.Where(p => p.ProductName.Contains(txtProductName.Text));
grvProductInfo.DataSource = productsInfo;
grvProductInfo.DataBind();
}
if (conditon2) query = from p in query where ... select p;
...
if (conditonn) query = from p in query where ... select p;
query = from p in query orderby ... select p;
return query.ToList();
[解决办法]
我的做法是跟3楼类似,不过我是在前台生成一个List<Expression>集合,然后通过bll传给dal去遍历where,最后在return
[解决办法]
Contains
[解决办法]
该回复于2010-12-24 16:22:05被版主删除
[解决办法]
var productinfo = from p in pro where p.ProductName.Contains("aa") select p;
public List<tb_stuInfo> Search(string stuName,string stuNum)
{
DC_StudentDataContext dc_stuCtx = new DC_StudentDataContext();
var query = from r in dc_stuCtx.tb_stuInfo
select r;
if(!string.IsNullOrEmpty(stuName))
{
query.Where(c => c.stuName == stuName);
}
if (!string.IsNullOrEmpty(stuNum))
{
query.Where(c => c.stuNum == stuNum);
}
return query.ToList();
}