具有存储过程的复杂Linq表达式

时间:2016-04-15 01:14:37

标签: c# sql-server linq asp.net-mvc-4 stored-procedures

我是MVC4的新手,我正在研究一个复杂的模型:一个包含IList类型属性的模型以及基本类型(字符串和整数)的属性。类型IList的属性应使用存储过程,基元类型使用常规链接查询。以下是该模型的代码:

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)存储过程返回第二个查询的用户转录表列表?

感谢。

2 个答案:

答案 0 :(得分:0)

我不是EF中存储过程的专家,但我已经看到了两种方法。

  • 例如,请查看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#中重写存储过程,第一种似乎是最好的方法。祝你好运!

答案 1 :(得分:-2)

首先,尝试使用以下链接。

https://www.linqpad.net

它给了我很多帮助。

其次,我认为List必须留在里面

fees
covered

name
shown


amount
shown