我决定快速了解一下 LINQ 方面的内容,而不是仅仅使用直接的 foreach 循环,但我在让它工作时遇到了一些麻烦,主要是由于我认为的数据类型。
到目前为止,我已经得到了这个;
var selectedSiteType = from sites in siteTypeList
where sites.SiteTypeID == temp
select sites;
siteTypeList 是 SiteType 的列表。我正在尝试找到一个特定的(我已经用变量“temp”谴责了它)。
然后我如何使用这个选定的 SiteType 作为 SiteType?当我尝试将“selectedSiteType”传递给另一个函数时,如下所示;
mSiteTypeSub.EditSitetype(selectedSiteType);
注意:我尝试提供索引,就好像 selectedSiteType 是列表/数组一样,但这也不起作用,我收到以下错误:
Argument 1: cannot convert from
'System.Collections.Generic.IEnumerable<DeviceManager_take_2.SiteType>' to
'DeviceManager_take_2.SiteType'
我错过了什么吗?也许是某种演员?就像我说的,我对此很陌生,并且正在努力解决这个问题。很有可能我的整个概念都是错误的,而且我让自己出丑了!
提前干杯。
Use First / 第一或默认 / Single / 单一或默认从集合中获取特定类型的项目。
var value = selectedSiteType.First();
// returns the first item of the collection
var value = selectedSiteType.FirstOrDefault();
// returns the first item of the collection or null if none exists
var value = selectedSiteType.Single();
// returns the only one item of the collection, exception is thrown if more then one exists
var value = selectedSiteType.SingleOrDefault();
// returns the only item from the collection or null, if none exists. If the collection contains more than one item, an exception is thrown.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)