Generic List is not coming
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.
精彩评论