如何初始化将用于存储查询结果的var [英] How to initilize a var that is going to be used for storing query result

查看:76
本文介绍了如何初始化将用于存储查询结果的var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉这个简单的问题!如何初始化将用于存储查询结果的var(db.query(...)或db.querysingle(...))?

I am sorry for this simple question! How to initilize a var that is going to be used for storing query result (db.query(...) or db.querysingle(...))?

var queryResult;



此代码将导致错误,因为var未启动。当我给它null值时,我得到这个错误:

不能将null赋给隐式类型的局部变量


This code will cause error because the var is not initilized. And when I give it null value I get this error:
Cannot assign null to an implicitly-typed local variable

推荐答案

这取决于查询类型。

如果使用Database.Query方法,则可以将变量初始化为

It depends on the query type.
If you use the Database.Query method, you can initialize the variable as
IEnumerable<dynamic> queryResult = null;





否则,如果你使用Database.QuerySingle方法,最好的方法是



Otherwise, if you use the Database.QuerySingle method, the best way is

dynamic queryResult = null;


Var是一种隐式类型。因此,您必须在声明它的位置分配值。它使用C#编程语言中的任何类型别名。别名类型由C#编译器确定。你不能为它指定null。
Var is an implicit type. So, you will have to assign it value where you declare it. It aliases any type in the C# programming language. The aliased type is determined by the C# compiler. You can not assign null to it.


这篇关于如何初始化将用于存储查询结果的var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆