简体   繁体   中英

ASP.NET Select Method Using Entity framework Stored Procedure

I have ListView like this

<asp:ListView ID="ListView1" ItemType="test.Project"  
     SelectMethod="ListView1_GetData" runat="server">

I am trying to set the select method to a stored procedure using EF. the select method like this

public IQueryable<Test.Project> ListView1_GetData()
{
    using (DREntities2 db=new DREntities2())
    {
        return db.GetLatestProjects().AsQueryable();
    }
}

I get this error:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)

If I remove the .AsQueryable() , I get this error:

Cannot implicitly convert type 'System.Data.Objects.ObjectResult' to 'System.Linq.IQueryable

Here is the definition of the GetLastestProjects_Result as returned by DREntities2 .GetLatestProjects() :

public partial class GetLatestProjects_Result 
{ 
   public int ProjectID { get; set; } 
   public string Title { get; set; } 
   public string ShortDescr { get; set; } 
   public string Full_Descr { get; set; } 
   public int ProCatID { get; set; } 
   public bool Marquee { get; set; } 
}

Your stored procedure is returning ObjectResult<Test.GetLatestProjects_Result> and you are trying to convert it to IQueryable<Test.Project> . To do this you need to convert using the Linq Select method. Assuming Test.GetLatestProjects_Result and Test.Project have the same properties:

public IQueryable<Test.Project> ListView1_GetData()
{
    using (DREntities2 db=new DREntities2())
    {
        return db.GetLatestProjects().Select(p => new Test.Project
        {
            ProjectId = p.ProjectId,
            Title = p.Title,
            ShortDescr = p.ShortDescr,
            Full_Descr = p.Full_Descr,
            ProCatID = p.ProCatID,
            Marquee = p.Marquee
        }).AsQueryable();
    }
}

A little late to the game, but others may be able to do the following. Assuming that Test.Project is an EntityType, in the Model Browser change the return type of your stored procedure in the EF Model to Test.Project instead of the default GetLatestProjects_Result "complex type" that is created.

Then, in your query you can take the result to a typed list, then call AsQueryable on the result.

Here is an example that calls an SP named GetAllRequestsByWorkflowState which returns TravelRequestSummaryView entities:

private IQueryable<TravelRequestSummaryView> GetApprovalRequests(int approvalState)
{
    IQueryable<TravelRequestSummaryView> trsv = null;
    try
    {
        var res = db.GetAllRequestsByWorkflowState(approvalState)
            .ToList<TravelRequestSummaryView>();
        trsv = res.AsQueryable();
    }
    catch (Exception ex)
    {
        sqlTraceManager.WriteTraceIf(ex, User.Identity.Name);
    }
    return trsv;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM