IntelliSense 在实现 LINQ 后没有给我选择属性的选项
为什么在实现 LINQ 后,IntelliSense 不给我选择 Glasses 集合属性(即 itemToDelete)的选项。
这是我的 C# LINQ:
public static void DeletePicturesFromHrdDisc(List<Glasses> Temp, int GlassID)
{
var itemToDelete = (from item in Temp
where item.GlassesID==GlassID
select item);
itemToDelete.(dosen't give me the optipn to select Properties of the Glasses class)
}
这是我的 Glasses 类:
public class Glasses
{
public string Brand { get; set; }
public string SmalPicture { get; set; }
public string BigPicture { get; set; }
public string color { get; set; }
public string FrameType { get; set; }
public int GlassesID { get; set; }
public string NameGlasses { get; set; }
public string Collection { get; set; }
}
但是如果我使用 .First() 或 .FirstorDefault() 扩展,我可以看到 Glasses 属性,如下所示:
知道为什么我无法访问第一个版本中的属性以及为什么只有当我使用 .First() 或时我才能在 IntelliSense 中看到它们我的 LINQ 上的 .FirstorDefault() 扩展。
先感谢您!
Why IntelliSense dosen't give me option to select properties of the Glasses collection(i.e itemToDelete) after implimenting the LINQ.
Here is my C# LINQ:
public static void DeletePicturesFromHrdDisc(List<Glasses> Temp, int GlassID)
{
var itemToDelete = (from item in Temp
where item.GlassesID==GlassID
select item);
itemToDelete.(dosen't give me the optipn to select Properties of the Glasses class)
}
here is my Glasses class:
public class Glasses
{
public string Brand { get; set; }
public string SmalPicture { get; set; }
public string BigPicture { get; set; }
public string color { get; set; }
public string FrameType { get; set; }
public int GlassesID { get; set; }
public string NameGlasses { get; set; }
public string Collection { get; set; }
}
But I can see Glasses properties if i use .First() or .FirstorDefault() extensions as follows:
Any idea why can't I access the properties in the first version and why i see them in IntelliSense only if i use .First() or .FirstorDefault() extensions on my LINQ.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您将Where/Select应用于Temp时,您不仅仅得到一个结果(可能有多个结果匹配列表。返回,您需要在使用其属性之前从列表中提取实际对象。
where item.GlassesID==GlassID
,编译器无法确定),因此它不返回 Glasses 的单个对象实例,而是返回 IEnumerable<眼镜>。简单地说,您会得到一个 List如果您确定最多只返回一个结果,则可以使用
SingleOrDefault ()
返回 IEnumerable 中的 Glasses 实例,如果为空则返回 null,或者Single()
返回 Glasses 实例IEnumerable,如果不存在则抛出错误。如果有多个结果,两者都会抛出异常。使用
First()
或FirstOrDefault()
意味着如果您不小心得到如果有多个结果,在这种情况下,您将得到第一个结果,一个随机结果。对于“itemToDelete”来说也许不是一件好事,您可能想知道您的假设是否错误。When you apply Where/Select to Temp, you're not just getting a single result (there could be more than one result matching
where item.GlassesID==GlassID
, the compiler cannot be sure) so it does not return a single object instance of Glasses but an IEnumerable<Glasses>. Simplified, think that you get a List<Glasses> back and you need to extract the actual object from the List before using its properties.If you definitely know there's at most only one result returned, you can use
SingleOrDefault()
which returns the Glasses instance in the IEnumerable or null if it's empty, orSingle()
which returns the Glasses instance in the IEnumerable and throws an error if there isn't one. Both throw an exception if there is more than one result.Using
First()
orFirstOrDefault()
would mean that if you accidentally get more than one result, you'll in this case get the first one, a random one. Perhaps not a good thing for an "itemToDelete", you'd probably want to know if your assumption is wrong.顺便说一句,您可以将鼠标悬停在 Visual Studio 中的
var
上来查看表达式的类型。在本例中,
itemToDelete
是Temp
中Glasses
的IEnumerable
,其GlassesID<
GlassID
的 /code>。假设您的GlassesID
在Temp
中实际上是唯一的,则它只有一项可以使用.Single()
或提取。 SingleOrDefault()。
编辑:正如其他人指出的那样,如果您想检查,
.Single()
而不是.First()
是正确的方法。仅针对一件物品。BTW, you can hover over
var
in Visual Studio to see the type of an expression.In this case,
itemToDelete
is anIEnumerable<Glasses>
ofGlasses
inTemp
with aGlassesID
ofGlassID
. Assuming yourGlassesID
is actually unique inTemp
, it will only have one item which you can extract with.Single()
or.SingleOrDefault()
.EDIT: As others have pointed out,
.Single()
, not.First()
is the right method to use if you want to check for exactly one item.在您的情况下,LINQ 查询始终会生成
IEnumerable
类型的项目序列。即使序列中只有一个元素,它仍然是一个序列。序列本身只能枚举,它没有任何其他属性。要获取元素的属性,您首先必须提取元素。这正是First()
所做的。它返回序列的第一个元素。如果您的查询始终应该生成一个项目,而任何其他结果都是错误,您应该使用
Single()
而不是First()
。如果可以接受零个或一个元素,您可以使用SingleOrDefault()
并检查结果是否为null
。LINQ queries always produce a seqence of items, of type
IEnumerable<Glasses>
in your case. Even if there is only one element in the sequence, it is still a sequence. The sequence itself can only be enumerated, it does not have any other properties. To get at the elements' properties you first have to extract an element. That's exactly whatFirst()
does. It returns the first element of the sequence.If your query always should produce exactly one item and any other result is an error you should use
Single()
instead ofFirst()
. If zero or one elements are acceptable you can useSingleOrDefault()
and check the result fornull
.因为您的第一个版本是 Glasses 的集合,而不是 Glasses 对象本身。
First()
或FirstOrDefault()
返回 Glasses 的单个实例,因此属性可用。Because your first version is a collection of Glasses, not a Glasses object itself.
First()
orFirstOrDefault()
return a single instance of Glasses, thus the properties are available.因为第一个()只会给出一个项目。所以编译器为它提供了智能感知。由于第一个版本是 IEnumerable,因此它具有对象集合。你需要迭代它。
as the first() will give only a single item. so the compiler is giving intellisense for it. as the 1st version is
IEnumerable<Glasses>
so it has collection of objects. you need to iterate it.