簡體   English   中英

從Linq中的存儲過程返回對象

[英]Return object from Stored procedure in Linq

我有一個linq表達式,我需要更改為存儲過程。 此linq表達式從dbml(表)返回定義的對象。

事實是,當我調用存儲過程(比如說SP_test)時,它返回一個SP_testResult數組,而我希望它返回一個已定義的表作為對象。

如果我在dbml中更改了SP的返回類型,則當我檢查結果時,它不會返回任何內容,但是如果我自己運行SP,它將返回一個集合。

有沒有定義退貨類型的方法? 說SP_test返回一個Client對象? (這是我的dbml模型中的表),還是應該將SP_testResult映射到Client對象?

我想從這里開始:

this.bookings = db.Bookings
        .FilterUser(_main.Identity.GetUser())
        .Where(x => x.ProductId == PackageContent.ProductId && x.CampaignId == PackageContent.CampaignId && x.ClientId == PackageContent.ClientId)
        .Where(x => Math.Abs((x.DateCreated - PackageContent.DateCreated).Days) < daysRange)
        .ToList()
        .Union(db.Bookings.Where(x => PackageContent.ExternCode == x.BussinessRefernce))
        .OrderBy(x => x.DateCreated);

對此:

this.bookings = db.SP_SearchSimilarBookings.toList();

在這兩種情況下,this.bookings都是Bookings類的一個數組。

也許考慮做這樣的事情將您的SP結果映射到您的自定義對象:

//create your connection string
string Conn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

using (SqlConnection connection = new SqlConnection(Conn))
{
    //add your SP to the SqlCommand
    SqlCommand cmd = new SqlCommand("dbo.MyProcedure", connection);
    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter myParam = new SqlParameter("@YourParam", //Your SqlDbType);
    myParam.Value = //your value;
    cmd.Parameters.Add(myParam);

    //Add more parameters if applicable...

    SqlDataReader dr = command.ExecuteReader();

    if (dr.HasRows)
    {
        while (dr.Read())
        {
            //map to your custom object here by instantiating one and mapping 
            //the SqlDataReader columns to your custom object properties.
        }
    }
 }

暫無
暫無

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

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