tags:

views:

50

answers:

1
   public IQueryable<Story> FindAllStories(){
        var stories = (from s in db.Stories
                       orderby s.DateEntered descending
                       select new Story
                       {     
                            Title = s.Title,
                            UserName = s.aspnet_User.UserName
                        }
                       );

        return stories;
    }

I need to pass this as IQueryable so the pagination helper I found online can only pull the items I need. The problem is that at runtime when the helper tries to do source.Count(); the compiler isn't a happy camper because it's an 'explicit contruction of an entity type query'.

How would I alter this LINQ to SQL method so this does not happen?

Also, to help me grasp this, why does the previous code not work and this one does?

   public IQueryable<Story> FindAllStories(){
        var stories = (from s in db.Stories
                       orderby s.DateEntered descending
                       select s);

        return stories;
    }

Update

I'm beginning to think the way to accomplish this (verified it works) is to create a POCO called UserStory. The new class has 2 properties: one of type Story and the other string UserName. I can then return an IQueryable of UserStory without a problem.

That's great; however, I still don't get why that method would work and my other doesn't. The other is adding a property of string UserName to my partial class Story and passing that object between layers. What's the difference?

A: 

The following link is to a blog post tat describes an issue similar to yours. It seems like the solution was to return a type that inherets from Story instead of a Story type:

http://devlicio.us/blogs/derik%5Fwhittaker/archive/2008/04/25/linq2sql-explicit-construction-of-entity-exception.aspx

Hope this helps.

Waleed Al-Balooshi
Here's the thing I don't get about that -- my Story is a partial class where I have added the property UserName. Now, if I create an entirely new Class (similar to the article): UserStory that has 2 properties: Story type and string UserName my code works. Why? What's the difference between the two?
Beavis
Maybe the answer is in this comment from that article: "Make sure the entity you want to create has no primary key (in the designer) and then it works just fine." Maybe because my method of creating a new class works because the class itself doesn't have a primary key...it contains a Story type and string UserName. However, just adding string UserName to the partial class Story means the Story has a primary key...hmm, not sure if I'm off base here.
Beavis