通用列表不会出现

发布于 2024-12-07 21:39:32 字数 908 浏览 0 评论 0原文

此示例使用 where 查找所有缺货的产品。

public void Linq2()
{
    List<Product> products = GetProductList();

    var soldOutProducts =
        from p in products
        where p.UnitsInStock == 0
        select p;

    Console.WriteLine("Sold out products:");
    foreach (var product in soldOutProducts)
    {
        Console.WriteLine("{0} is sold out!", product.ProductName);
    }
}

结果:

售完产品:

  • 安东厨师的秋葵浓汤已售完!
  • 爱丽丝羊肉卖完了!
  • 图林格州腊肠已售完!
  • 戈贡佐拉泰利诺 (Gorgonzola Telino) 已售罄!
  • 珀斯馅饼卖完了!

上面的例子是我从MSDN Samples得到的,这是Simple2,问题当我输入 List 时,Products 未显示在 Intellisense 中。当我手动输入时,出现以下错误:

仅限赋值、调用、递增、递减和新建对象表达式 可以用作语句

我能做些什么来解决这个问题?

This sample uses where to find all products that are out of stock.

public void Linq2()
{
    List<Product> products = GetProductList();

    var soldOutProducts =
        from p in products
        where p.UnitsInStock == 0
        select p;

    Console.WriteLine("Sold out products:");
    foreach (var product in soldOutProducts)
    {
        Console.WriteLine("{0} is sold out!", product.ProductName);
    }
}

Result:

Sold out products:

  • Chef Anton's Gumbo Mix is sold out!
  • Alice Mutton is sold out!
  • Thüringer Rostbratwurst is sold out!
  • Gorgonzola Telino is sold out!
  • Perth Pasties is sold out!

The above example i got from MSDN Samples, this is Simple2, the problem is when I enter List<Products>, Products is not showing in Intellisense. When I enter it manually, I get the following error:

Only assignment, call, increment, decrement and new object expression
can be used as a statement

What can I do to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

成熟的代价 2024-12-14 21:39:32

好的,

您的问题是您复制了链接的源。但此源代码既不包含 Product 也不包含 GetProductList() 的定义

请查看示例 此处 - 它拥有您需要的一切:

List<string> fruits =
    new List<string> { "apple", "passionfruit", "banana", "mango", 
                    "orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

Ok,

your problem is that you copied the linked source. But this source does not contain the definitions for neither Product nor GetProductList()

Please take a look at the example here - it has everything you need:

List<string> fruits =
    new List<string> { "apple", "passionfruit", "banana", "mango", 
                    "orange", "blueberry", "grape", "strawberry" };

IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}
烟凡古楼 2024-12-14 21:39:32

那是因为你没有定义类。您添加类定义。您可以将类写入同一个文件中,添加一个新的类文件并将 Product 的定义放入其中。

That's because you don't have the class defined. You to add the class definition. You could write the class in the same file, add a new class file and put the definition of Product in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文