Reputation: 61
The function implements the search of a product in the list. I am getting an error in the return type inside the if loop. What should be the return type if the product is found and what should be the return type if the productname is not found??The value should be returned to which variable in main() method??
namespace Sample
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
public static Product SearchProductsByName(string ProductsName, List<Product> products)
{
Product pd;
pd= new Product();
foreach (Product rs in products)
{
if (ProductsName == rs.Name)
{
return pd;
}
}
}
}
static void Main(string[] args)
{
Product res=new Product();
Console.WriteLine("Enter the name of the product");
string pname = Console.ReadLine();
res=SearchRestaurantsByName(pname , products);
}
Upvotes: 2
Views: 2451
Reputation: 93601
The compile error you mention is down to you not returning a value if no match was found. Even if there is always a match the compiler does not know that, so it has to complain about any code being able to exit without a return value:
public static Product SearchProductsByName(string ProductsName, List<Product> products)
{
Product pd;
pd= new Product();
foreach (Product rs in products)
{
if (ProductsName == rs.Name)
{
return pd;
}
}
return null; // <<< Return a value if nothing was found
}
...as you are writing in C# is is about time you started using LINQ to your advantage.
Iteration, searching etc are what LINQ was designed for. By using LINQ you write less code and become more productive, with less errors (assuming the standard practice that more lines = more errors):
Your code could simply become:
static void Main(string[] args)
{
Product res=new Product();
Console.WriteLine("Enter the name of the product");
string pname = Console.ReadLine();
res = products.SingleOrDefault(x=>x.Name == ProductName);
}
The funny x=>x.Name == ProductName
is called a lambda expression. It is basically like a little function that gets passed each value (as x
) and returns a boolean result of the comparison x.Name == ProductName
. Treat the x
as if it was a C# this
in your head if that helps.
SingleOrDefault
simply returns the first item where the lambda expression returns true. If no items match it will return a "default", which for an object is null
.
There are a number of LINQ methods you can use in this case. Where(x=>x.Name == ProductName)
would return a collection (actually an iEnumerable) of all matching items. FirstOrdefault()
does almost exactly the same as SingleOrDefault
. The only difference being that SingleOrdefault
implies there is only one match (which is the case with your product data). FirstOrDefault
implies there may be multiple matches, but you don't care which one comes first.
Basically I strongly recommend you start to learn the benefits of LINQ and stop writing functions to perform trivial single-line query operations.
Upvotes: 1
Reputation: 14432
Why do you need a separate method for that? Use LinQ to find an object by a certain property, like this:
string name = Console.ReadLine();
var foundPropduct = products.FirstOrDefault(p => p.Name == name);
If there is a product found, foundProduct
will be the result, otherwise it will be null
.
Upvotes: 2
Reputation: 186803
Put it like that:
public static Product SearchProductsByName(string ProductsName, List products) {
foreach (Product rs in products)
if (ProductsName == rs.Name)
return rs; // <- The product found
return null; // <- There's no such a product in the list
}
EDIT: As for Main
method, something like that is expected:
static void Main(string[] args) {
// You should declare and fill in the products list
// where you'll be looking for the product
List<Product> products = new List<Product>() {
//TODO: Replace it with the actual products
new Product() {Name = "Product A"},
new Product() {Name = "Product B"},
new Product() {Name = "Product C"}
};
// Ask user for the product name
Console.WriteLine("Enter the name of the product");
string pname = Console.ReadLine();
// The product user's looking for
Product res = SearchRestaurantsByName(pname, products);
//TODO: change this for required response to user
if (res != null)
Console.WriteLine("The product is found");
else
Console.WriteLine("The product is not found");
}
Upvotes: 6
Reputation: 47794
return products.Where( m => m.Name == ProductsName ).FirstOrDefault();
A little explanation to the above code.
FirstOrDefault is almost the same as First. The difference is how it handles empty collections. If a collection is empty, it returns the default value for the type. This method simplifies code. In some programs it eliminates exceptions.
In this case it will return null when no product have a matching ProductName
Upvotes: 3
Reputation: 62498
you can do like this:
var prod =products.SingleOrDefault(x=>x.Name == ProductName);
return prod;
Your method would be like:
public static Product SearchProductsByName(string ProductsName, List<Product> products)
{
Product pd = products.SingleOrDefault(x=>x.Name == ProductName);
return pd;
}
In my opinion you don't need to make a seperate method, instead of it use SingleOrDefault
Upvotes: 1