简体   繁体   中英

Add stored proc and call it from a service

  1. I create a simple stored procedure with some joins with the customer table and other related tables, which takes in two parameters. I can execute this SP in SQL and works.
  2. I drag and drop this SP to my DBML file and recompile.
  3. I add the below code in order to call the SP and return it in a List

     public IQueryable<Entities.Customer> AllCustomerRanges(int CId, int ItemID) { List<Entities.Customer> c = myDataContext.spCustomerRanges(CId, ItemID).ToList(); } 

This gives me the error:

Cannot implicitly convert type 'System.Collections.Generic.List< spCustomerRangesResult>' to 'System.Collections.Generic.List< Entities.Customer>'

Now i dont have a class spCustomerRangesResult but after some research I'm puzzled if i have done something wrong or if i need to implement a class with all the properties that the Customer class has (which sounds a little long winded) or if i've just made an error.

Any idea of how i can call a SP which shows the data in a List?

new class spCustomerRangesResult automatically generated based on sp result, you should convert it to Entities.Customer like this:

      public IQueryable<Entities.Customer> AllCustomerRanges(int CId, int ItemID)
        {
            var c = myDataContext.spCustomerRanges(CId, ItemID).ToList();

            if (c == null)
                return null;

            var customers = c.Select(a => new Entities.Customer
            {
                FirstName=a.spResultFirstName,
                LastName = a.spResultLastName
                //this just example conversion, change it as needed. 
            });


            return customers;

        }

please note, that I return IQueryable even though the approach that you take when using ToList() but yet returning IQuerybale may not be needed. I dont know all details so this only to show how to convert but the whole method may need re-factoring.

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