[求助]关于linq查询语句

sparkle2009 2012-02-22 09:51:59
我想写一个图书查询的方法,大概如下
现在的问题是:我排序OrderBy写死的是用的book类Name属性,但是我不想写死,由
参数决定,比如说,
参数传入author就是OrderBy(t =>t.author);
参数传入keyword1就是OrderBy(t =>t.keyword1);


这个该如何实现啊。。。。苦思不得解中,渴望得到帮助,多谢:)
-------------------------------------
public class book
{
string name;
string author;
string keyword1;
string keyword2;
string keyword3;
int ISBN;
DataTime publishTime;
}
-------------------------------------
public class dbQuery
{
public List<QueryResult> QueryBooks(string orderKey)
{
BooksDB context = new BooksDB("name=BooksDB");
List<QueryResult> results = new List<QueryResult>();
using (context)
{
results = (from result in context.QueryResults
select result).OrderBy(t =>t.name).ToList();
}
return results;
}
}
...全文
67 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
貌似没有回答我的问题啊
[Quote=引用 1 楼 hefeng_aspnet 的回复:]

http://www.cnblogs.com/xbs729/archive/2010/03/27/1698529.html
[/Quote]
q107770540 2012-02-23
  • 打赏
  • 举报
回复
public List<QueryResult> QueryBooks(string orderKey)
{
BooksDB context = new BooksDB("name=BooksDB");
List<QueryResult> results = new List<QueryResult>();
using (context)
{
results = (from result in context.QueryResults
select result).OrderBy(t =>GetPropertyValue(t,orderKey)).ToList();
}
return results;
}



private static object GetPropertyValue(object obj, string property)
{
System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);
return propertyInfo.GetValue(obj, null);
}
q107770540 2012-02-23
  • 打赏
  • 举报
回复
csdn_aspnet 2012-02-22
  • 打赏
  • 举报
回复
http://www.cnblogs.com/xbs729/archive/2010/03/27/1698529.html

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧