Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All

Am new in Generic , i dont know how to convert this one

public partial class Student
    {
        public int Id { get; set; }
        public string StudentCode { get; set; }
        public string StudentName { get; set; }
        public string Department { get; set; }
        public string Address { get; set; }
    }


public virtual ObjectResult<sp_StudentDetails_Result> sp_StudentDetails(string action)
       {
           var actionParameter = action != null ?
               new ObjectParameter("Action", action) :
               new ObjectParameter("Action", typeof(string));

           return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<sp_StudentDetails_Result>("sp_StudentDetails", actionParameter);
       }


now i tried with this one

List<Student> student = new List<Student>();
           using (StudentEntities myDBEnty = new StudentEntities())
           {
               student = myDBEnty.Students.ToList();
               student = myDBEnty.sp_StudentDetails("SELECT");
           }
          return Json(student, JsonRequestBehavior.AllowGet);


and i want to return and i got error like

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS0029	Cannot implicitly convert type 'System.Data.Entity.Core.Objects.ObjectResult<WebApplication2.Models.Data.sp_StudentDetails_Result>' to 'System.Collections.Generic.List<WebApplication2.Models.Data.Student>'	WebApplication2	E:\MVC Test\WebApplication2\WebApplication2\Controllers\HomeController.cs	46	Active


how to do this one

Thanks

What I have tried:

tried in googling and did some sample i can't make it
Posted
Updated 20-Feb-17 22:42pm
v2
Comments
prasanna.raj 21-Feb-17 4:25am    
if i use List error like this

Severity Code Description Project File Line Suppression State
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<webapplication2.models.data.sp_studentdetails_result>' to 'System.Collections.Generic.List<webapplication2.models.data.student>' WebApplication2 E:\MVC Test\WebApplication2\WebApplication2\Controllers\HomeController.cs 47 Active
F-ES Sitecore 21-Feb-17 4:44am    
It depends what the StudentDetails SP does. If it is returning rows from the Student table then you need to configure the SP to re-use the Student object to return the results from as by default it creates a new class called sp_StudentDetails_Result to hold the results of the SP. If your SP is not returning just rows from the Student table but adding\removing some columns then it gets more complicated and you'll just have to create a List of Student objects from your list of sp_StudentDetails_Result objects by creating a Student object and copying the properties over in code, or use a mapping framework to do this.

Basically though, Entity Framework doesn't know your SP returns Student items, so it has created a new class and you can't simply convert sp_StudentDetails_Result to Student even if they have the same properties, you need to manually copy the properties to configure the SP to re-use the Student object in its results. Google for articles on SPs and EF and how you do these things will be explained.
prasanna.raj 21-Feb-17 5:21am    
thank you ...

any article if have pls share me
F-ES Sitecore 21-Feb-17 5:51am    
This will show you how to map the SP to your own class rather than using the one EF creates for you

http://blogs.microsoft.co.il/gilf/2009/03/13/mapping-stored-procedure-results-to-a-custom-entity-in-entity-framework/

That might not be the solution to your problem though, the nest solution relies on data you haven't provided, we don't know what is in your SP.
prasanna.raj 21-Feb-17 6:33am    
thank you F-ES Sitecore ... i ll try

1 solution

I have not done work with EF for a while but it was possible to generate code to wrap for POCOs (Plain Old Clr Objects) so do recasting was required.

Otherwise, change:
C#
student = myDBEnty.sp_StudentDetails("SELECT");
To:
C#
var result = myDBEnty.sp_StudentDetails("SELECT");
and set a breakpoint after. Now use the Locals window to view the contents of result variable. Now you can see what work needs to be done.

This is a good resource for you: entityframeworktutorial.net[^]
 
Share this answer
 
v3
Comments
prasanna.raj 21-Feb-17 4:36am    
thanks for your replay Graeme_Grant

ya your right if use VAR it will be work but i want to return like this

return Json(student, JsonRequestBehavior.AllowGet);

how to do
Graeme_Grant 21-Feb-17 4:39am    
That was not your question.
prasanna.raj 21-Feb-17 4:43am    
sry i tried with var while debugging its working fine but my concept want to return or want to get in "student"
Graeme_Grant 21-Feb-17 4:47am    
F-ES Sitecore goes into a little bit more detail than I but I've also suggested that the way that you have set up EF needs rethinking. Please re-read my solution and check out the link provided for more information. Also there are many other articles out there on how to map POCOs with EF (Entity Framework) so no recasting or manual mapping is required.
prasanna.raj 21-Feb-17 5:22am    
k thank you i ll try ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900