【问题标题】:Complex Linq expression with a stored procedure带有存储过程的复杂 Linq 表达式
【发布时间】:2016-04-15 01:14:37
【问题描述】:

我对@9​​87654321@ 还很陌生,我正在研究一个复杂的模型:一个包含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) 存储过程在第二个查询中返回用户记录列表?

谢谢。

【问题讨论】:

  • 感谢 Rob 的意见。我们还没有进入 EF6。我正在使用 EF 4。

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


【解决方案1】:

我不是 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# 中重写存储过程,第一个似乎是最好的方法。祝你好运!

【讨论】:

    【解决方案2】:

    首先,尝试使用下面的链接。

    https://www.linqpad.net

    这对我帮助很大。

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

    new EditUserModel() { UserTranscripts = tr }
    

    【讨论】:

    • linqpad的广告与问题无关,不需要。而且我在这里看不到与调用存储过程相关的任何内容。
    猜你喜欢
    • 2019-09-14
    • 2014-04-25
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多