>Hi,
>
>I am working on an ASP.NET MVC app using Web API.
>
>I am trying to create the ability to pull back a subset of data to display on a page.
>
>So I have my repository defined like this:
>
>
using IBCPackTrack.Models;
>using System;
>using System.Data.Entity;
>using System.Linq;
>
>namespace IBCPackTrack.Data
>{
> public class DeliveryHistoryRepository : GenericRepository<DeliveryHistory>
> {
> public DeliveryHistoryRepository(DbContext context)
> : base(context)
> {
>
> }
>
> public IQueryable<DeliveryHistory> GetByRouteId(Guid id)
> {
> return DbSet.Select(dh => new DeliveryHistory
> {
> del_pk = dh.del_pk,
> del_time = dh.del_time,
> });
>
> }
> }
>}
>
>This is giving me a syntax error under the Select in "DbSet.Select":
>
>'System.Data.Entity.DbSet' does not contain a definition for 'Select'
>
>I'm not sure why as this code works fine in another application that I am using as an example.
>
>Basically what I want to do is accept a key and use that key to produce a subset of data from DeliveryHistory like this SQL would produce:
>
>SELECT DeliveryHistory.*
>FROM DeliveryHistory
>INNER JOIN RunSheetDetails on del_rndfk = rnd_pk
>INNER JOIN RunsheetHeaders on rnd_rnhfk = rnh_pk
>WHERE rnh_rtefk = @RouteId
>
>Any help appreciated!
DbSet is a type which corresponds to your tables. Your repository should have a property of type DbSet< DeliveryHistory> that you would use as your starting point. That is, unless your GenericRepository class defines DbSet as a property.
As for your query, there are a couple of things you will want to change:
1) DeliveryHistory is an entity object, and entity framework doesn't like you using those objects for selecting a subset of the data. You'll need to define a new class to do the subset of data.
2) Your query isn't filtering the data at all.
Your query should look something like this:
public IQueryable< DeliveryHistory> GetByRouteId(Guid id)
{
return context.DeliveryHistories // The context property that controls access to our data
.Where(dh => dh.RunSheetDetail.RunSheetHeader.rnh_rtefk == id) // Filter the data by the routes
.Select(dh => new DeliveryHistorySimple() // Select a subset of the data. If you want all of the DeliveryHistory entity, get rid of the select function
{
del_pk = dh.del_pk,
del_time = dh.del_time,
});
}