开发者

Generic List is not coming

开发者 https://www.devze.com 2023-04-09 19:06 出处:网络
This sample uses where to find all products that are out of stock. public void Linq2() { List<Product> products = GetProductList();

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开发者_如何学C 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?


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);
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号