我将一个表单序列化为json对象,如下所示
Fruit: Apple,
StrawberyParam: "test",
AppleParam: 1,
PeachParam: 3,
Car: Porsche
BugattiParam:3,
PorscheParam: "gas",
ToyotaParam: "go",
AnotherParam: "test1"
基本上,用户选择水果和汽车,参数在其他属性中描述。
我正在考虑编写不同的where子句并将它们连接起来创建一个字符串,然后像这样应用。tolist:
StrawberryWhere = ....
AppleWhere = ....
PeachWhere = ....
PorscheWhere = ....
.....
然后这样写:
MyQuery = fromClause + switch based on Fruit and Car selected
(ie. AppleWhere + PorscheWhere) + selectClause;
MyQuery.ToList();
是这样做的吗?我用的是linq-to-sql,我对这个框架不是很熟悉,谢谢你的建议
你不会使用字符串,你会这样做:
Expression<Func<TSource, bool>> appleWhere = x => x.Fruit == Fruits.Apple;
Expression<Func<TSource, bool>> porscheWhere = x => x.Car == Cars.Porsche;
// ...
var result = jsonObjects.Where(appleWhere).Where(porscheWhere).ToList();
看看Dynamic LINQ:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx