簡體   English   中英

添加存儲的過程並從服務中調用

[英]Add stored proc and call it from a service

  1. 我創建了一個簡單的存儲過程,其中包含與customer表和其他相關表的一些連接,其中包含兩個參數。 我可以在SQL中執行此SP並起作用。
  2. 我將此SP拖放到我的DBML文件中並重新編譯。
  3. 我添加以下代碼以調用SP並將其返回到列表中

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

這給了我錯誤:

無法將類型'System.Collections.Generic.List <spCustomerRangesResult>'隱式轉換為'System.Collections.Generic.List <Entities.Customer>'

現在我沒有一個spCustomerRangesResult類,但是經過一些研究,如果我做錯了什么,或者是否需要使用Customer類具有的所有屬性(聽起來有點長)來實現一個類,我會感到困惑。我剛剛犯了一個錯誤。

關於如何調用顯示列表中數據的SP的任何想法嗎?

基於sp結果自動生成的新類spCustomerRangesResult,應將其轉換為Entities.Customer,如下所示:

      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;

        }

請注意,即使您使用ToList()時采取的方法但仍不需要返回IQuerybale,我也會返回IQueryable。 我不知道所有詳細信息,所以這只是為了說明如何進行轉換,但是整個方法可能需要重構。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM