Luke101
Luke101

Reputation: 65328

How do I return an IEnumerable?

I am trying to return an IEnumerable. Here is my code:

public static IEnumerable<int> GetAnswersIDs(int id)
{
    using (var context = new MvcApplication4.Entity.test2Entities1())
    {
        return (from p in context.post
                  where p.post_parentid == id && p.post_isdeleted == false
                  select new
                  {
                      p.post_id
                  }).ToList();
    }           
}

Is it possible to return an IEnumerable object?

Upvotes: 0

Views: 3556

Answers (5)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104841

I think you should use ToArray or ToList (both Array<T> and List<T> implement IEnumerable<T>), since when you return, the context is disposed, I am nearly sure you will get an exception if you use AsEnumerable leaving the enumeration itself for later steps:

public static IEnumerable<int> GetAnswersIDs(int id)
{
    using (var context = new MvcApplication4.Entity.test2Entities1())
    {
        return (from p in context.post
                  where p.post_parentid == id && p.post_isdeleted == false
                  select p.post_id).ToArray();
    }           
}

BTW, I think in your case using linq is more verbose than the non-linq version:

public static IEnumerable<int> GetAnswersIDs(int id)
{
    using (var context = new MvcApplication4.Entity.test2Entities1())
    {
        return context.post.
          Where(p => p.post_parentid == id && !p.post_isdeleted).
          Select(p => p.post_id).
          ToArray();
    }           
}

As a side note, you might want to consider reformatting the field names in your model to meet the naming convensions in .NET.

Upvotes: 2

Amir Karimi
Amir Karimi

Reputation: 5521

You dont need to use any To[Anything] method or something like that. Your mistake is your select way. The correct code is this:

public static IEnumerable<int> GetAnswersIDs(int id)
{
    using (var context = new MvcApplication4.Entity.test2Entities1())
    {
        return (from p in context.post
                  where p.post_parentid == id && p.post_isdeleted == false
                  select p.post_id);
    }           
}

As you see you created an anonymous class which you dont need because you want just an int value.

-- EDIT --

You will be faced to an ObjectDisposed Exception. Becase when you use the returned IEnumerable object it need to use the context which disposed in the GetAnswersIDs method.

So you could return a List instead of IEnumerable or you should define the context out of your GetAnswersIDs method.

This is the code with List

public static List<int> GetAnswersIDs(int id)
{
    using (var context = new MvcApplication4.Entity.test2Entities1())
    {
        return (from p in context.post
                  where p.post_parentid == id && p.post_isdeleted == false
                  select p.post_id).ToList();
    }           
}

Upvotes: 3

BrokenGlass
BrokenGlass

Reputation: 161012

I think you are looking for AsEnumerable():

However after looking at your code you have to fully materialize the results from the query so using ToList() is appropriate - otherwise you will get an exception when you later try to enumerate the results, since it will try to access the DB on a disposed DB context.

Upvotes: 1

archil
archil

Reputation: 39501

For the base IEnumerable return, yield return is used

Upvotes: 0

Numan
Numan

Reputation: 3948

I don't think you'll need a select new {}
select p.post_id should be sufficient! Then you can surround it with an .AsEnumerable()

Upvotes: 0

Related Questions