视图显示整个SQL查询,而不显示结果集

时间:2019-07-17 19:24:53

标签: c# asp.net-mvc

我的控制器中有两个linq查询,两个查询都旨在返回一个字符串。但是,我的视图显示了从Linq查询生成的整个SQL查询。

型号:

public class DummyClass
{
    public string myMessage { get; set; }
    public string myInitials { get; set; }
}

控制器:

public ActionResult EmployeeSignature()
{
    using (MessageInfo db = new MessageInfo())
    {
        var message = db.SiteMessage_MyPay
                        .Where(x => x.active)
                        .Select(x => x.msgText).ToString();

        using (DocumentReviewed database = new DocumentReviewed())
        {
            var document = database.Document_Reviewed
                                   .Where(x => x.FileNumber == "whatever")
                                   .Select(x => x.Initials).ToString();

            var model = new DummyClass()
                {
                    myMessage = message,
                    myInitials = document
                };

            return View(model);
        }
    }
}

查看:

@model MyPayTC.Models.DummyClass
@{
    ViewBag.Title = "EmployeeSignature";
}

@Model.myInitials

@Model.myMessage

网站显示:

SELECT [Extent1].[Initials] AS [Initials] 
FROM [dbo].[Document_Reviewed] AS [Extent1] 
WHERE '105057' = [Extent1].[FileNumber]

1 个答案:

答案 0 :(得分:2)

您应该像这样使用.ToString( ).FirstOrDefault()等,而不是使用ToList()

var message = db.SiteMessage_MyPay
                .Where(x => x.active)
                .Select(x => x.msgText).FirstOrDefault();

或者,如果要检索某些列,则可以使用ToArray()ToList()