IntelliSense 在实现 LINQ 后没有给我选择属性的选项

发布于 2025-01-04 21:59:23 字数 1129 浏览 4 评论 0原文

为什么在实现 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

牵你的手,一向走下去 2025-01-11 21:59:23

当您将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, or Single() 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() or FirstOrDefault() 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.

雾里花 2025-01-11 21:59:23

顺便说一句,您可以将鼠标悬停在 Visual Studio 中的 var 上来查看表达式的类型。

在本例中,itemToDeleteTempGlassesIEnumerable,其 GlassesID< GlassID 的 /code>。假设您的 GlassesIDTemp 中实际上是唯一的,则它只有一项可以使用 .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 an IEnumerable<Glasses> of Glasses in Temp with a GlassesID of GlassID. Assuming your GlassesID is actually unique in Temp, 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.

治碍 2025-01-11 21:59:23

在您的情况下,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 what First() 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 of First(). If zero or one elements are acceptable you can use SingleOrDefault() and check the result for null.

Spring初心 2025-01-11 21:59:23

因为您的第一个版本是 Glasses 的集合,而不是 Glasses 对象本身。 First()FirstOrDefault() 返回 Glasses 的单个实例,因此属性可用。

Because your first version is a collection of Glasses, not a Glasses object itself. First() or FirstOrDefault() return a single instance of Glasses, thus the properties are available.

只是偏爱你 2025-01-11 21:59:23

因为第一个()只会给出一个项目。所以编译器为它提供了智能感知。由于第一个版本是 IEnumerable,因此它具有对象集合。你需要迭代它。

 var itemToDelete = (from item in Temp
                       where item.GlassesID==GlassID
                       select item);     

foreach(var i in itemToDelete )
{
   i.SomeProperty // do some thing with i ;
}

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.

 var itemToDelete = (from item in Temp
                       where item.GlassesID==GlassID
                       select item);     

foreach(var i in itemToDelete )
{
   i.SomeProperty // do some thing with i ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文