views:

682

answers:

2

I would like to know how to define a returntype in a function in following situation.

I've got a products and I was returning all information or one product at a time.

as you can see in my function defined below.

public static Products GetProducts(int pid) 
{
    var pro = from p in context.Products
              select p;

    if(pid > 0)
        pro =  pro.where(p => p.ProductID ==pid)

    return (Products)p;
}

the problem is its give me casting error. as you can see what i want to achieve is based on my parameter its give me a result set. some time bunch of products & some time single product. i m new to linq so any help would be appreciated.

The error is Unable to cast object of type 'System.Data.Objects.ObjectQuery`1[TTDCore.Theatres]' to type 'TTDCore.Theatres'

when i m binding it to gridview. here is a code

Products p = Class1.GetProducts(0);

GridView1.DataSource = p;
GridView1.DataBind();
+3  A: 

You want to return IEnumerable<Product>, which represents an iterable (or enumerable) of objects of type Product. LINQ in general is all based around this generic type, so it's generally what you want to return as a result of a query.

I believe your code should be fixed to become something like this:

public static IEnumerable<Products> GetProducts(int pid) 
{
    var pro = from p in context.Products
              select p;

    if(pid > 0)
        pro =  pro.Where(p => p.ProductID == pid)

    return pro;
}

Let me know if you meant something else in your question. I wasn't totally sure what precisely you were searching for.

Noldorin
yes this is exactly what i was looking for. just a quick one i tried this Response.Write(p.ProductName);its works find just wana know if its a right way. thanx for ur quick response.
Andy
Ah, good. :) And yeah, your suggested way of returning a single record should work fine. Depending on the situation, either the First/FirstOrDefault or Single/SingleOrDefault extension method is appropiate. The latter (Single) will throw an exception if there is *more* than one match, whereas the former (First) of course just returns the first, and then stops enumerating. The `OrDefault` bit just means to return the default value (null for reference types) if there are no matches, as opposed to throwing an exception.
Noldorin
A: 

I like to be explicit with Linq queries and lambdas. I suggest defining your function as List<Product> (or IEnumerable<Product>), then adding .ToList() to your where statement. I assume that the Products type is some sort of collection?

Matt Sherman
Product is Table in my sql Server Database = Entity Class in Entity Framework. what do you suggest is the best way to play with this scenario.
Andy
Well, I don't think you need to define the *return type of the function* as Products. You are specifying Products in the query, which is enough. So your return type should be an IEnumerable or List, if I understand the goal correctly. They will bind to your GridView just fine.
Matt Sherman