为何使用LINQ进行此警告 [英] Why this Warning with LINQ

查看:87
本文介绍了为何使用LINQ进行此警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用linq进行第一次打击。代码现在编译/工作正常,但结果变量incedentName下有黄色的squigley行:

警告1没有'As'子句的变量声明;假定的对象类型。

我理解这个警告,如果保留原因会导致一些问题吗?



基本上,将ID传递给列表并只返回一个名字。由于只返回了一个名称,有没有其他方法可以在没有'for each'循环的情况下从变量中获取名称?



I am attempting my first whack using linq. The code compiles/works fine for now, but there is the yellow squigley line under the result variable incedentName:
Warning 1 Variable declaration without an 'As' clause; type of Object assumed.
I understand the warning, will this cause some problems down the road if left as is?

Basically, pass an ID to the list and return only one name. Since there is only one name being returned, is there any other way to get the name out of the variable without the 'for each' loop?

Dim returnIncidentName As String
Try
    'find type name from list
    Dim incedentName = From t As IncedentNotifications.IncedentTypes In incidentTypeList
                            Where t.IncedentTypeID = returnedTypeID
                            Select t.IncedentType

    For Each n As String In incedentName
        returnIncidentName = n
    Next

推荐答案

AFAIK绝对没有问题让对象未声明。但是你根本不需要声明任何变量,看看



AFAIK there is absolutely no problem leaving the object undeclared. But you don't need to declare no variable at all, look

returnIncidentName = (
From t As IncedentNotifications.IncedentTypes In incidentTypeList 
Where t.IncedentTypeID = returnedTypeID 
Select t.IncedentType).First()





是的,您可以在查询结束时使用First()方法。



但如果有可能返回null,请使用FirstOrDefault()并且您将获得NULL,避免因黄色错误屏幕而感到警觉。



And yes, you can just use First() method on the end of the query.

but if there is a chance of returning null, use FirstOrDefault() and you will have a NULL, avoiding be alarmed with a yellow error screen.


请参阅:

http://msdn.microsoft.com/en-us/library/bb384665.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx [ ^ ]。



通常,可以有不同类型的推理。 VB,甚至VB.NET,允许基于在赋值运算符右侧使用的立即常量的默认类型的推理这样的坏事。这是一种最好避免的不良做法。同时,推断可以基于右侧调用的方法的类型或实际先前已知类型的变量来完成。这是一种非常合理和合法的方法,也用于不错的语言(不,我甚至不能称VB.NET是一种体面的语言,尽管有明显的改进)。当具有相同名称和不同返回类型的不同方法的存在产生歧义时,编译器会告诉您它。在所有情况下,变量名称都是严格声明的,尽管不是直接声明。



不幸的是,VB.NET代码不能直接和简单地区分彼此。我的建议:总是用VB.NET声明类型,不要乱用选项。是的,这是更麻烦的方式,但它比不那么明显的推断更好。



我希望你明白你想保持开发不受任何警告的影响。然后允许在工作代码中等同于切换强大且有用的警告警告机制。始终清理每一个警告。



-SA
Please see:
http://msdn.microsoft.com/en-us/library/bb384665.aspx[^],
http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx[^].

Generally, there can be different kinds of inference. VB, even VB.NET, allows such a bad thing as inference based in the default type of the immediate constant used on the right side of the assignment operator. This is a bad practice which is best avoided. At the same time, the inference can be done based on the type of the method called on the right side or actually previously declared variable with known type. This is a very reasonable and legitimate approach, which is also used in decent languages (no, I cannot call even VB.NET a decent language, despite of apparent great improvements). When the existence of different method with the same name and different return types creates the ambiguity, the compiler tells you about it. In all cases, the variable name is strictly declared anyway, albeit not directly.

Unfortunately, VB.NET code cannot directly and simply distinguish one from another. My advice: always declare types with VB.NET, don't mess with options. Yes, this is more bothering way, but it's better than not so obvious inference.

And I hope you understand that you want to keep development free from any kinds of warnings. Allowing then in working code is equivalent to switching the powerful and useful warning mechanism of warning off. Always clean up every single warning.

—SA


这篇关于为何使用LINQ进行此警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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