为什么使用块不能安全地初始化var?(Why can a use block not safely initialize a var?)
为什么会出现编译错误?
val autoClosable = MyAutoClosable() var myVar: MyType autoClosable.use { myVar= it.foo() } println(myVar) // Error: Variable 'myVar' must be initialized
也许编译器只看到
{ myVar= it.foo() }
作为一个传递给另一个函数的函数,并且不知道何时甚至是否会被执行?但是,由于
use
不仅仅是一个功能,而是Kotlin替代Java的资源尝试,一些关于它的特殊知识将是合适的,不是吗? 现在,我被迫用一些虚拟值初始化myVar
,这根本不符合Kotlin的精神。Why does this give a compile error?
val autoClosable = MyAutoClosable() var myVar: MyType autoClosable.use { myVar= it.foo() } println(myVar) // Error: Variable 'myVar' must be initialized
Maybe the compiler just sees
{ myVar= it.foo() }
as a function that is passed to another function and has no knowledge about when or even if it will be executed?But since
use
is not just a function but Kotlin's replacement for Java's try-with-resource, some special knowledge about it would be appropriate, wouldn't it? Right now, I am forced to initializemyVar
with some dummy value, which is not in the spirit of Kotlin at all.
原文:https://stackoverflow.com/questions/45238746
最满意答案
当你说:-
var _db = new PortOfTroyCustomers.Models.PortOfTroyContext();
编译器推断赋值右侧的表达式类型,这在C#中称为隐式类型。
现在,您正在尝试分配,这样的查询,我们右边有
System.Data.Entity.DbSet
,左边有System.Linq.IQueryable
,它们是不同的类型: -IQueryable<Accommodation> query = _db.Accommodations;
因此,您需要一个明确的类型转换: -
IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;
When you say:-
var _db = new PortOfTroyCustomers.Models.PortOfTroyContext();
The compiler infers the type of the expression to the right of the assignment, this is known as implicit type in C#.
Now, you are trying to assign, your query like this, we have
System.Data.Entity.DbSet
on right &System.Linq.IQueryable
on left which are different types:-IQueryable<Accommodation> query = _db.Accommodations;
Thus, you need an explicit typecast like this:-
IQueryable<Accommodation> query = (IQueryable<Accommodation>)_db.Accommodations;
相关问答
更多-
var reports = dbContext.Product.ToList(); if (searchField == "StoreNum") { int storeNum; int? stnum = int.TryParse(searchString, out storeNum) ? storeNum : (int?)null; reports = reports .Where( ...
-
看起来DocsList_List1的类型为IOrderedEnumerable (正如@Emre在注释中指出的那样)。 要使代码编译,您必须将DocsList_list1的声明更改为兼容类型(可能是IEnumerable ),或者将linq查询的结果IOrderedEnumerable为IOrderedEnumerable 。 一种(有点难看)的方式来做后者: var DocsFirst = (from t in db.tble_presentation where dt ...
-
rec变量隐式输入为DbQuery
对象(因为tblOrders是DbQuery 对象,而DbQuery .AsNoTracking返回相同类型的对象)。 所以,你的var声明看起来像编译器: DbQuery rec = (bNoTracking ? tblOrders.AsNoTracking() : tblOrders); 但是, Where仅定义为返回IQueryable 对象,该对象不一定适合作为DbQuery 对象 ... -
db.Banks的类型为DbSet 。 该类未实现ICollection接口。 将方法的返回类型更改为IQueryable
或IEnumerable 。 public class BankRepository : IBankRepository { HefContext db = new HefContext(); public IQueryable GetAll() { return db.Banks; } } ... -
您的查询返回System.Linq.IQueryable,但您尝试将其分配给Modal.Staff。 更改查询以返回单个结果: MyQuery = CompiledQuery.Compile((Modal.Entities U, string StaffNo) => U.Staff.Where(a => a.StaffNo == StaffNo).FirstOrDefault()); Your query returns System.Linq.IQueryable, but you ar ...
-
您不需要在演员表中包含IQueryable
: var tp90web = (from p in context.vw_TP90Web select p).Cast (); 请注意,如果查询未返回vwTP90Web实例,则在运行时仍会失败。 You don't need to include the IQueryable in your cast: var t ... -
也许这会让你接近......我不确定你如何建立你的关系,但如果Usuario >> Relaciones >> Cursos那么你可以做这样的事情.. public ActionResult Index(int? idCursos) { var model = db.Usuario; if (idCursos.HasValue) { model = model.Where(a => a.Relaciones.Any(a => a.Cursos.idCurso == i ...
-
尝试使用AsQuearable()。 但正如我在评论中指出的那样,这可能会产生影响。 Try using AsQuearable(). But as I noted in my comment, that can have repercussions.
-
当你说:- var _db = new PortOfTroyCustomers.Models.PortOfTroyContext(); 编译器推断赋值右侧的表达式类型,这在C#中称为隐式类型。 现在,您正在尝试分配,这样的查询,我们右边有System.Data.Entity.DbSet ,左边有System.Linq.IQueryable ,它们是不同的类型: - IQueryable
query = _db.Accommodations; 因此,您需要一个明确的类型转换: ... -
主要问题是您实际上并没有执行查询,因此您只剩下IQueryable而不是实际结果,因此您将收到错误。 public IEnumerable
GetInventoryLocations() { IEnumerable results = null; using (DataContext ctx = new DataContext()) { results = (from a ...