views:

611

answers:

3

i need Enumarable value from below class but give error


   public static  Enumerable  LoadDataByName(string name)
        {
            List<StuffDepartman> Stuff = (List<StuffDepartman>)HttpContext.Current.Session["Stuffs"];
            var stuffs = from s in Stuff select s;

             stuffs = from s in Stuff where s.Name = name select s.Name;

            return stuffs.AsEnumerable();
        }

But Give me error: System.Linq.Enumerable': static types cannot be used as return types

+1  A: 

System.Linq.Enumerable is a static class with a bunch of extension methods defined on it. You perhaps meant to return IEnumerable<string> instead.

Barry Kelly
A: 

Enumerable is a static class, what you want to do is return an IEnumerable:

    public static  IEnumerable<string>  LoadDataByName(string name)
    {
      //do stuff
    }

I'm assuming s.Name in your example is a string, which is why the method should return IEnumerable<string>. If it's another type, then change it to that type....

EDIT: Ok, I guess it's not a string. Change it to:

    public static  IEnumerable<StuffDepartman>  LoadDataByName(string name)
    {
      //do stuff
    }
BFree
if i add your codes,error change....
Phsika
Error1: Cannot implicitly convert type 'string' to 'bool'
Phsika
Error2:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<BLL.StuffDepartman>' to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion exists (are you missing a cast?)
Phsika
+1  A: 

In .NET 3.5, there exists a static Enumerable existing in System.Linq, which contains extension methods for manipulating IEnumerables - this is not what you want to (or clearly can) return. Change it to IEnumerable, which is the non-generic enumerable class (and what I think you intend), and all should work fine.

Even better, use the generic version of IEnumerable, as such:

public static IEnumerable<StuffDepartman> LoadDataByName(string name)
{
    var stuffs = (List<StuffDepartman>)HttpContext.Current.Session["Stuffs"];

    return (from s in stuffs where s.Name == name select s.Name);
}

Also, note that you don't need the call to AsEnumerable before returning, since List<T> implements IEnumerable<T>, and the former can be implicitly casted to the latter. The = needed to be changed to ==, since you want to use the equality rather than assigment operator here. The other changes are just tidying up.

Noldorin
your codes give me error:The non-generic type 'System.Linq.Enumerable' cannot be used with type arguments
Phsika
you can not do that "Enumerable<StuffDepartman>"
Phsika
Oops. Sorry, missed out the 'I' - it should be IEnumerable.
Noldorin
you correct but "s.Name = name" give me: Cannot implicitly convert type 'string' to 'bool'
Phsika
Change that to `s.Name = name`. In C#, = is the assignment operator, while == is the equality operator.
Noldorin
Related to last comment of me: s.Name defined "string" also name defined as string.
Phsika
@ykaratoprak: Yeah, I presumed that. If I were to guess, you come from a BASIC/VB background, where both the equality and assignment operators are `=` - this is not the case in C#.
Noldorin
oops sorry you are correct!
Phsika