创建一个列表来包含 LINQ 结果?

发布于 2024-10-20 22:35:47 字数 529 浏览 2 评论 0原文

如何定义 resultList 以允许存储数据库行?

List<WhatType?> resultList = new List<WhatType?>();

if(someBool){
    resultList = db.table.where(a=>a.value>0).ToList();
}
else{
    resultList = db.table.where(a=>a.values<=0).ToList();
}

我似乎无法执行以下操作,因为 var 需要初始值:

var result;

if(someBool){
    result = db.table.where(a=>a.value>0).ToList();
}
else{
    result = db.table.where(a=>a.values<=0).ToList();
}

这里有更好的方法来解决我的范围问题吗?

谢谢!

How do I define resultList to allow storing database rows?

List<WhatType?> resultList = new List<WhatType?>();

if(someBool){
    resultList = db.table.where(a=>a.value>0).ToList();
}
else{
    resultList = db.table.where(a=>a.values<=0).ToList();
}

I can't seem to do the following because var needs an initial value:

var result;

if(someBool){
    result = db.table.where(a=>a.value>0).ToList();
}
else{
    result = db.table.where(a=>a.values<=0).ToList();
}

Is there a better way to solve my scope issues here?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拥抱影子 2024-10-27 22:35:47

将其设置为空列表:

var result = new List<WhatType?>();

如果使用 .NET 4.0,您可以使用动态类型,但是您需要对其进行强制转换:

var result  = new List<dynamic>();
result = (List<dynamic>)db.Blogs.Where(b => b.ID == 1);

Set it to an empty list:

var result = new List<WhatType?>();

If using .NET 4.0 you could utilize the dynamic type, you would need to cast it however:

var result  = new List<dynamic>();
result = (List<dynamic>)db.Blogs.Where(b => b.ID == 1);
许你一世情深 2024-10-27 22:35:47

在 Linq2SQL 中,结果大多数时候是 IQueryable 的。当您将鼠标悬停在Where子句上时,IntelliSense将显示其类型

In Linq2SQL, results are most of the time IQueryable. When you hover mouse over your Where clause, IntelliSense will display its type

宁愿没拥抱 2024-10-27 22:35:47

就我个人而言,除非必要,否则我不喜欢使用 var。因此,将其定义为:

List<table> resultList = new List<table>();

if(someBool){
    resultList = db.table.where(a=>a.value>0).ToList();
}
else{
    resultList = db.table.where(a=>a.values<=0).ToList();
} 

其他答案没有抓住重点,因为您想知道如何定义类型。简而言之,将其定义为您的返回类型。如果你想要一个整数列表,你的类型是列表。需要注意的一点是,您可以将 table 替换为您的表名称,因此如果是客户,您会这样做:

List<customers> resultList = new List<table>();

if(someBool){
    resultList = db.customers.where(a=>a.value>0).ToList();
}
else{
    resultList = db.customers.where(a=>a.values<=0).ToList();
} 

要扩展@KeithS的答案,您现在可以执行以下操作:

List<customer> result = someBool ? result = db.customer.where(a=>a.value>0).ToList() 
: db.customer.where(a=>a.values<=0).ToList();

希望这一切都有意义:)

Personally, I do not like using var unless i have to. So define it as this:

List<table> resultList = new List<table>();

if(someBool){
    resultList = db.table.where(a=>a.value>0).ToList();
}
else{
    resultList = db.table.where(a=>a.values<=0).ToList();
} 

The other answers miss the point as you want to know how to define the type. In simplest terms, define it to what ever your return type is. If you want a list of Integer, your type is List. The point to note about this is that you replace table with your table name, so if it was customers you would do:

List<customers> resultList = new List<table>();

if(someBool){
    resultList = db.customers.where(a=>a.value>0).ToList();
}
else{
    resultList = db.customers.where(a=>a.values<=0).ToList();
} 

To extend @KeithS's answer you can now do the following:

List<customer> result = someBool ? result = db.customer.where(a=>a.value>0).ToList() 
: db.customer.where(a=>a.values<=0).ToList();

Hope it all makes sense :)

錯遇了你 2024-10-27 22:35:47

您必须使用 db.table 中元素的类型

You have to use the type of an element from db.table

茶底世界 2024-10-27 22:35:47

看起来您正在使用 Linq2Sql。如果是这种情况,那么您当然知道查询的结果类型,因为在任何一种情况下您都是在同一个表上执行查询。这些不是返回的匿名类型,您的 ORM 已将每个表映射到相应的类。

顺便说一句,您的示例会让人们相信您在任何一种情况下都在查询同一个表。如果情况不正确,那么您应该更新示例以使其正确。

It looks as though you are using Linq2Sql. If that is the case then you certainly know the result type of your query because you are performing the query on the same table in either case. These aren't anonymous types being returned, your ORM has mapped each table to a corresponding class.

BTW, your example would lead one to believe that you are querying the same table in either case. If that is not true then you should update the example to be correct.

秋叶绚丽 2024-10-27 22:35:47

那么,您使用什么 ORM 技术?如何获取db对象? table 收集的元素是什么类型?

如果您根本不知道,您可以将此特定语句组合成一个条件(三元)表达式:

var result = someBool 
    ? result = db.table.Where(a=>a.value>0).ToList() 
    : db.table.Where(a=>a.values<=0).ToList();

不适用于所有情况;例如,如果您需要返回结果或将其作为参数传递给另一个方法,您必须知道您期望的收集类型。但是,对于这一小块,您可以让编译器推断类型。

编辑:如果没有关于变量db及其属性table是什么的更多信息,我无法给您比这更好的答案。我的第一个问题是,您使用什么在对象和数据库之间移动?是 Linq2SQL 吗? Linq 实体?休眠?

在所有情况下,Linq 提供程序都会为您提供 IQueryable 引用,它是表达式树的基础,它将被评估并消化为本机查询语言(最常见的是 SQL)。在 99% 的情况下,IQueryable 是与所生成的对象类型强类型化的。如果将上述代码插入到程序中,并将鼠标悬停在 var 关键字上,您将获得一个工具提示气球,其中包含推断类型的名称和描述。正如我所说,这可能是 IQueryable,并且该工具提示中会有一个脚注说明本例中的 T 是什么。

如果 IQueryable 不是泛型,则收集的类型将只是对象。但是,如果是这种情况,则Where 方法将无法编译,因为Object 没有Value 或Values 属性。

Well, what ORM technology are you using? How do you get the db object? What is the type of the collected element of table?

If you simply don't know, you can combine this particular statement into a conditional (ternary) expression:

var result = someBool 
    ? result = db.table.Where(a=>a.value>0).ToList() 
    : db.table.Where(a=>a.values<=0).ToList();

Won't work for all cases; for instance if you need to return result or pass it as a parameter to another method, you have to know the collected type you're expecting. But, for this small piece, you can let the compiler infer the type.

EDIT: I can't give you a better answer than this without more information as to what the variable db and its property table are. My first question was, what are you using to go between objects and the database? Is it Linq2SQL? Linq for Entities? NHibernate?

A Linq provider will, in all cases, give you an IQueryable reference, which is the base of the expression tree which will be evaluated and digested into the native query language (most often SQL). In 99% of cases, that IQueryable is strongly typed to the type of object being produced. If you plug the above code into your program, and mouse over the var keyword, you will get a tooltip balloon with the name and description of the inferred type. As I say, that will likely be IQueryable<T>, and there will be a footnote in that tooltip as to what T is in this case.

If the IQueryable is NOT generic, then the collected type will simply be Object. However, if that's the case, then the Where method will not compile because Object does not have a Value or Values property.

ま昔日黯然 2024-10-27 22:35:47
resultList = db.table.where(a=>a.value>0 && someBool || a.value<=0 && !someBool).ToList();
resultList = db.table.where(a=>a.value>0 && someBool || a.value<=0 && !someBool).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文