开发者

How do I return an IEnumerable?

开发者 https://www.devze.com 2023-02-21 09:19 出处:网络
I am trying to return an IEnumerable. Here is my code: public static IEnumerable<int> GetAnswersIDs(int id)

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?


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();
    }           
}


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.


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.


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()


For the base IEnumerable return, yield return is used

0

精彩评论

暂无评论...
验证码 换一张
取 消