2016-04-15 36 views
0

我對MVC4相當新,我正在研究一個複雜模型:一個包含類型爲IList的屬性以及基本類型(字符串和整數)屬性的模型。 IList類型的屬性應使用存儲過程,並且基元類型使用常規鏈接查詢。下面是模型的代碼:使用存儲過程複雜Linq表達式

public class EditUserModel 
{ 
    public IList<UserTranscript> UserTranscripts { get; set; } 

    public int? PersonID { get; set; } 
    public string UserName { get; set; } 
    public string FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string LastName { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string StateCode { get; set; } 
    public string PostalCode { get; set; } 
    public string Phone { get; set; } 
    public string Email { get; set; } 
} 

下面是UserTranscript類的代碼:

public class UserTranscript 
{   
    public decimal Score { get; set; }   
    public DateTime CompletionDate { get; set; } 
    public string Status { get; set; } 
} 

這裏是我的方法:

public EditUserModel GetUserRecord(int personid) 
    { 
     //using (var db = new TceoModel.TceoContext()) 
     //{ 

      MyContext db = new MyContext(); 

      var user = (from p in db.People 
         from pu in db.PersonUsernames.Where(f => f.PersonID == p.UPID).DefaultIfEmpty() 
         from pe in db.PersonEmails.Where(a => a.PersonID == p.UPID).DefaultIfEmpty() 
         from pa in db.Addresses.Where(c => c.PersonID == p.UPID).DefaultIfEmpty()        
         from lnr in db.Activities.Where(y => y.ActivityID == un.ActivityID).DefaultIfEmpty() 
         from tr in db.uspTranscripts(personid) 
         where p.UPID == personid 

         select new EditUserModel 
         { 
          PersonID = p.UPID, 
          UserName = pu.Username, 
          Email = pe.Email, 
          FirstName = p.FirstName, 
          MiddleName = p.MiddleName, 
          LastName = p.LastName, 
          Address = pa.Address1, 
          City = pa.City, 
          StateCode = sc.StateAbbr, 
          PostalCode = pa.Zip, 
          Phone = pp.PhoneNumber 

         }).AsEnumerable().Select(s => new UserTranscript() { 

          **How to return a list of UserTranscripts using the stored procedure db.uspTranscripts(personid)** 

         }); 

我的問題是,我怎麼能使用db.uspTranscripts(personid)存儲過程返回第二個查詢中的成績單用戶列表?

感謝。

+0

感謝Rob的輸入。我們還沒有進入EF6。我正在使用EF 4. –

回答

-2

首先,嘗試使用下面的鏈接。

https://www.linqpad.net

這對我幫助很大。

其次,我認爲有名單裏面呆

new EditUserModel() { UserTranscripts = tr } 
+1

linqpad的廣告與問題無關,不需要。我沒有看到任何與調用存儲過程有關的東西。 – Rob

0

我在EF存儲過程的專家,但我已經看到了2種方法來做到這一點。

  • 例如查看https://msdn.microsoft.com/en-us/library/bb399357(v=vs.110).aspx。他們有自己的示例存儲過程寫成了一項功能,然後你可以使用它像

    // Usage 
    .AsEnumerable().Select(s => db.uspTranscripts(s)); 
    
    // Stored Procedure 
    [Function(Name="dbo.CustOrderTotal")] //probably dbo.uspTranscripts in your case 
    [return: Parameter(DbType="Int")] 
    public int CustOrderTotal([Parameter(Name="CustomerID", DbType="NChar(5)")] string customerID, [Parameter(Name="TotalSales", DbType="Money")] ref System.Nullable<decimal> totalSales) 
    { 
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID, totalSales); 
        totalSales = ((System.Nullable<decimal>)(result.GetParameterValue(1))); 
        return ((int)(result.ReturnValue)); 
    } 
    
  • ,或者您可能能夠做到這一點像的代碼在這傢伙的回答的最後一行,你居然達到和搶存儲過程使用它https://stackoverflow.com/a/20973919/4875338

第一個似乎是做的最好的方式,如果你願意重寫C#中的存儲過程。祝你好運!