Linq to SQL获取多列 - c#

我正在开发一个asp.net MVC Web应用程序,其中使用Linq to Sql通过jquery和ajax从数据库获取结果。我的模型有以下代码

 public IEnumerable<string> getComments(long problemID)
        {

            var comment = from c in _objectModel.Comments
                          where c.ProblemID == problemID
                          select new { c.EmpID, c.CommentText, c.Time }.ToString();

            return comment;

        }

我的控制器有以下代码

public string GetComments(string problemid)
        {
            List<string> collection =  _discussionRepository.getComments(Convert.ToInt32(problemid)).ToList();
            string comments = null;
            foreach (string item in collection)
            {
                comments += item + "\n";

            }

            return comments; 



        }

在我看来

$("#btnPostComment").click(function () {
  var strdata = {
           problemID: $("#problemID").val(),
           commentText: $("#_1").val(),
           empID: $("#empID").val(),
           agree: 0,
           disagree: 0
        };
        $.ajax({
            type: "POST",
            url: "<%= Url.Action("PostComment", "Discussion")  %>",
            data: strdata,
  error: function(msg){
                alert("error" + msg);
            },
            success: function (msg) {
                var id = { problemid :  $("#problemID").val()};
                 $.ajax({
                    type: "GET",
                    url: "<%= Url.Action("GetComments", "Discussion")  %>",
                    data:  id,
                    error: function(msg){
                         alert("error2" + msg);
                    },
                    success: function (msg) {

                        $("#commentdiv").html(msg);
                    }
                    });


            }
        });

我在asp页面中得到以下结果

{EmpID = 1,CommentText = sss,时间= 1/27/2012 2:20:49 AM} {EmpID = 1,CommentText = aaa,时间= 1/27/2012 2:46:07 AM} {EmpID = 1 ,CommentText = aaa,时间= 2012年1月27日2:50:23} {EmpID = 1,CommentText = Munazza,时间= 2012年1月22日2:58:29} {EmpID = 1,CommentText = Jawad ,Time = 1/27/2012 3:00:51 AM} {EmpID = 1,CommentText = xx,Time = 1/28/2012 11:56:59 AM} {EmpID = 1,CommentText = ss,Time = 1 / 28/2012 12:35:00 PM}

我想要不带花括号且不带属性的结果,即1 ss 1/27/2012

问候

参考方案

问题是您要在匿名类型上调用ToString。您现有的Comment模型类是否包含太多数据?如果没有,您可以使用:

public List<Comment> GetComments(long problemID)
{
    return _objectModel.Comments.Where(c => c.ProblemID == problemID)
                                .ToList(); // Force evaluation
}

然后,您需要更改控制器以将该List<Comment>转换为Javascript可以理解的AJAX。

如果您确实只想要没有属性的字符串,则可以将原始代码更改为:

public IEnumerable<string> GetComments(long problemID)
{
    var query = from c in _objectModel.Comments
                where c.ProblemID == problemID
                select new { c.EmpID, c.CommentText, c.Time };

    return query.AsEnumerable() // Do the rest locally
                .Select(c => string.Format("{0} {1} {2"}, c.EmpID,
                                           c.CommentText, c.Time));
}

您还应该将控制器代码更改为使用String.JoinStringBuilder-否则由于重复的字符串连接,您将遇到O(n2)问题。

LINQ to XML语法 - c#

我有一个简单的POCO类,用于保存从XML文件提取的数据,该XML文件定义如下:public class Demographics { public string FirstName { get; set; } public string LastName { get; set; } public string MiddleName { get; set; …

LINQ to SQL中的“无法识别的表达式节点数组索引linq”异常 - c#

这是我的LINQ,其中我将DateTime类型的字段与当前日期进行了比较- var srs = (from s in dcDistrict.ScheduledReportStatus where s.ReportConfigId.Equals(ConfigId) && s.Status.HasValue && s.Status…

如何处理LINQ to Entities仅支持无参数的构造函数和初始化程序[重复] - c#

This question already has answers here: Only parameterless constructors and initializers are supported in LINQ to Entities (14个回答) 4年前关闭。 因此,当我尝试从Web Api获取数据时会发生这种情况:ExceptionMessa…

Java中的OrderByDecending(LINQ)等效项 - java

嗨,我是一名使用Java的C#开发人员。问题很简单:我如何才能将下面的c#代码写入Java并仍能正常工作:myCoffeeList.OrderByDescending(x => x.Name?.ToLower()?.Trim() == sender.Text.ToLower()?.Trim())); 我的sender.Text基本上是一个文本框。文本的…

LINQ RemoveAll代替循环 - c#

我有两个for循环,用于从列表中删除项目。我正在为这些循环寻找等效的LINQ语句for (Int32 i = points.Count - 1; i >= 0; i--) { for (Int32 j = touchingRects.Count - 1; j >= 0; j--) { if (touchingRects[j].HitTest(po…