【问题标题】:How to use returned linq variable?如何使用返回的 linq 变量?
【发布时间】:2017-03-26 01:29:08
【问题描述】:

我决定快速了解一下 LINQ 方面,而不是直接使用 foreach 循环,但我在让它工作时遇到了一些麻烦,主要是由于我相信的数据类型。

到目前为止,我已经掌握了这个;

var selectedSiteType = from sites in siteTypeList
                                   where sites.SiteTypeID == temp
                                   select sites;

siteTypeList 是一个站点类型列表。我正在尝试找到一个特定的(我用变量“temp”谴责了它。

然后我如何使用这个选定的站点类型作为站点类型?当我尝试将“selectedSiteType”传递给另一个函数时,就像这样;

mSiteTypeSub.EditSitetype(selectedSiteType);

注意:我尝试提供索引,好像 selectedSiteType 是一个列表/数组,但这也不起作用,我收到以下错误:

Argument 1: cannot convert from 
'System.Collections.Generic.IEnumerable<DeviceManager_take_2.SiteType>' to 
'DeviceManager_take_2.SiteType' 

我错过了什么吗?也许是某种类型的演员?就像我说的那样,我对此很陌生,并且正在努力解决这个问题。很有可能我把整个概念都搞错了,而且我自欺欺人了!

提前干杯。

【问题讨论】:

  • 应该只有一个匹配项吗?这应该适合你:var selectedSiteType = siteTypeList.SingleOrDefault(s=&gt;s.SiteTypeID == temp);

标签: c# .net linq


【解决方案1】:

使用First / FirstOrDefault / Single / SingleOrDefault 从集合中获取特定类型的项目。

   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. 

【讨论】:

  • 我在从 LINQ 返回的 IEnumerable 上使用它?还是将其修改到我的查询本身的末尾?
  • @Shane.C 你可以这样做;结果是一样的。
【解决方案2】:

如果你的返回类型是单一的:

   var selectedSiteType = (from sites in siteTypeList
                                       where sites.SiteTypeID == temp
                                       select sites).SingleOrDefault();

如果是一个列表(可能不止一项):

 var selectedSiteType = (from sites in siteTypeList
                                       where sites.SiteTypeID == temp
                                       select sites).ToList();

这是您的查询中缺少的 SingleOrDefault / ToList。

【讨论】:

  • 为什么要投射 ToList?关于 ToList() 相对于 ToArray() 的优点一直存在一些争论,如果您只使用该值一次,您也可以将其保留为查询表达式。
  • 实际上,有很多文章和 .ToList / IENumerable 和 .ToArray 杂乱无章,性能差异如此之小,以至于它们基于用户偏好以及我的事实我反对“哦,我一定会永远使用一次”的心态。此外,我更喜欢使用 List 而不是 IENumerable/Array。
【解决方案3】:

肖恩,

我不会改进以前的答案。他们都是正确的。我会试着给你解释一下,以便你以后更好地理解它。

当您编写如下代码时会发生什么:

var selectedSiteType = from sites in siteTypeList
                               where sites.SiteTypeID == temp
                               select sites;

您不会将答案放入 var (selectedSiteType) 中,而是创建一个表达式树,仅在您实际使用它时(在 foreach 中或通过调用其中一种方法(如 . First()、.ToList()、SingleOrDefault() 等)。

from 语句的默认返回类型是 IEnumerable,但如果您调用 .First() 或 .SingleOrDefault() (等),您将深入了解该 IEnumerable 并获取特定项目。

我希望这可以帮助您更好地了解发生了什么。

让我知道我是否可以添加任何内容或者我是否有任何错误。

干杯,

最大

【讨论】:

  • 这实际上清除了很多! LINQ 似乎比繁琐的 foreach 循环要少得多。 ToList() 是否创建其项目的数据类型列表?还是通用列表?
  • @Shane.C 嗯,“通用”列表实际上是项目数据类型的列表。这将是一个不属于正确数据类型的非通用列表(即ArrayList)。自己尝试回答这个问题很容易。
  • 是的。我一问就明白了哈哈。已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多