IEnumerable不能与类型参数一起使用。

36

我希望使用数据列表来展示用户名以及其评论和每个评论下的附件。

我有一个名为ReviewList的用户控件用于显示评论和其中的efiles。但是在这行代码中出现了错误(s.tblDraft.Comments),并且在下面还有一个错误:

The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments

请帮忙,有什么问题吗?

private void Displayuser()
{
    var reviews =
      (from s in _DataContext.tblSends
       from u in _DataContext.Users

       where (s.DraftId == _Draftid) && (s.ToEmailId == u.ID)
       orderby u.Name
       select new
{
    userid = u.ID,
    username = u.Name,
    comments =s.tblDraft.Comments,
    w = s.tblDraft.Comments.SelectMany(q => q.CommentAttaches)

}).Distinct();

    DataList1.DataSource = reviews;
    DataList1.DataBind();

    var theReview = reviews.Single();

    DisplayReviews(theReview.comments, theReview.w);
}

private void DisplayReviews(IEnumerable<Comment> comments,
     IEnumerable<CommentAttach> w)
{
    ReviewList reviewList = (ReviewList)DataList1.FindControl("ReviewList1");
    reviewList.Comments = comments;
    reviewList.CommentAttachs = w;
    reviewList.DataBind();
}
2个回答

80

好东西!另外,如果你使用像Resharper这样的工具,它会为你建议这个修复方案。 - Dan Csharpster
嗨,Joachim,我尝试使用“System.Collections.Generic.IEnumerable<T>”,但它显示“找不到类型或命名空间名称'T'”。你有什么想法吗? - Michimcchicken
1
@Michimcchicken,T只是在声明中用于占位的。例如,如果您想要一个字符串的IEnumerable,您应该使用IEnumerable<string>,如果您想要一个整数的IEnumerable,您应该使用IEnumerable<int>等。 - Joachim Isaksson

10

你需要添加命名空间的导入:

using System.Collections.Generic;

@GertArnold 我是.NET的新手。所以对于被接受的答案中应该使用哪个语句并不明显。 - Aris
这确实是我在 Blazor 领域的答案。 - c-sharp-and-swiftui-devni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接