[求助]关于linq查询语句的一个问题

sparkle2009 2012-02-23 12:48:53
我想写一个图书查询的方法,大概如下
现在的问题是:我排序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;
}
}
...全文
96 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
不管怎样,深刻学习了好多知识,谢过了!

[Quote=引用 12 楼 nonocast 的回复:]

OK,老大,我理解错了,嘿嘿
[/Quote]
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
最后反射搞定了!谢谢帮助!!
[Quote=引用 10 楼 fangxinggood 的回复:]

引用 9 楼 nonocast 的回复:

见#2楼

引用 2 楼 nonocast 的回复:

C# code

class Program {
static void Main(string[] args) {
users = new List<User>{
new User{Name="AAA", Age=5},
new User{Name="BBB", Age=……
[/Quote]
nonocast 2012-02-23
  • 打赏
  • 举报
回复
OK,老大,我理解错了,嘿嘿
nonocast 2012-02-23
  • 打赏
  • 举报
回复
我觉得他只是要参数化,KISS
机器人 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 nonocast 的回复:]

见#2楼

引用 2 楼 nonocast 的回复:

C# code

class Program {
static void Main(string[] args) {
users = new List<User>{
new User{Name="AAA", Age=5},
new User{Name="BBB", Age=1},
new User{……
[/Quote]

这样还不如直接调,何必封装呢?哪里体现动态?
加上 if else 判断,就和我用 switch 是一样的了。

真动态要么用 Dynamic.cs (Emit来做),要么动态生成 Lambda Expression
msdn: http://msdn.microsoft.com/zh-cn/library/bb882637.aspx

推荐下个 Dynamic.cs 就都解决了,你可以直接写 OrderBy("xxx desc") 这样的语法。

nonocast 2012-02-23
  • 打赏
  • 举报
回复
见#2楼

[Quote=引用 2 楼 nonocast 的回复:]

C# code

class Program {
static void Main(string[] args) {
users = new List<User>{
new User{Name="AAA", Age=5},
new User{Name="BBB", Age=1},
new User{……
[/Quote]
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
lamba怎么做啊?

比如说入参传入了string是“author”
就要相当于调用了OrderBy(t => t.author);?

比如说入参传入了string是“name”
就要相当于调用了OrderBy(t => t.name);?


[Quote=引用 5 楼 nonocast 的回复:]

直接用lambda不就ok了,用反射就搞大了,呵呵
[/Quote]
机器人 2012-02-23
  • 打赏
  • 举报
回复
厄,忘了,Linq2EF 不能直接用反射。

要么 ToList() 之后再 OrderBy,要么去下个 Dynamic.cs

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
看到了。异常了。。。
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object, System.Object[])' method, and this method cannot be translated into a store expression.

[Quote=引用 4 楼 fangxinggood 的回复:]

引用 3 楼 iopoy 的回复:

是我简化了,真实case会非常多,这样罗列不是个办法啊。。。
引用 1 楼 fangxinggood 的回复:

你这也没复杂条件,可以直接写为:
C# code

public List<QueryResult> QueryBooks(string orderKey)
{
using (var context = new BooksDB……
[/Quote]
nonocast 2012-02-23
  • 打赏
  • 举报
回复
直接用lambda不就ok了,用反射就搞大了,呵呵
机器人 2012-02-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 iopoy 的回复:]

是我简化了,真实case会非常多,这样罗列不是个办法啊。。。
引用 1 楼 fangxinggood 的回复:

你这也没复杂条件,可以直接写为:
C# code

public List<QueryResult> QueryBooks(string orderKey)
{
using (var context = new BooksDB("name=BooksDB"))
{
……
[/Quote]

有没有看到我下面的那句话?用“反射”
sparkle2009 2012-02-23
  • 打赏
  • 举报
回复
是我简化了,真实case会非常多,这样罗列不是个办法啊。。。
[Quote=引用 1 楼 fangxinggood 的回复:]

你这也没复杂条件,可以直接写为:
C# code

public List<QueryResult> QueryBooks(string orderKey)
{
using (var context = new BooksDB("name=BooksDB"))
{
switch(orderKey)
{
cas……
[/Quote]
nonocast 2012-02-23
  • 打赏
  • 举报
回复

class Program {
static void Main(string[] args) {
users = new List<User>{
new User{Name="AAA", Age=5},
new User{Name="BBB", Age=1},
new User{Name="CCC", Age=3},
};
OrderBy<string>(t => t.Name);
Console.WriteLine("----------------------");
OrderBy<int>(t => t.Age);


Console.WriteLine("press any key to exit.");
Console.ReadLine();
}

private static void OrderBy<TKey>(Func<User, TKey> selector) {
users.OrderBy(selector).Select(p=>p).ToList().ForEach(t=>Console.WriteLine(t.Name));
}

private static List<User> users;
}
机器人 2012-02-23
  • 打赏
  • 举报
回复
你这也没复杂条件,可以直接写为:

public List<QueryResult> QueryBooks(string orderKey)
{
using (var context = new BooksDB("name=BooksDB"))
{
switch(orderKey)
{
case "author":
return context.QueryResults.OrderBy(r => r.author).ToList();
case "xxx":
return context.QueryResults.OrderBy(r => r.xxx).ToList();
default:
return context.QueryResults.ToList();
}
}
}


另外还可以用反射OrderBy

return context.QueryResults.OrderBy(r => r.GetType().GetProperty("Author").GetValue(r, null));

110,569

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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