本文介绍了为何使用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 1Variable 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

推荐答案

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.



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

10-30 09:17