【问题标题】:IEnumerable cannot be used with type argumentsIEnumerable 不能与类型参数一起使用
【发布时间】:2013-01-03 21:13:56
【问题描述】:

我想使用数据列表来显示用户名下方的每个评论的用户名及其 cmets 和附件。

我有一个用户控件ReviewList用于显示 cmets 和 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();
}

【问题讨论】:

    标签: asp.net linq


    【解决方案1】:

    编译器看到的类型是System.Collections.IEnumerable,它是非泛型 IEnumerable。您导入了该命名空间,因此这是编译器认为您尝试使用的类型。

    您尝试使用的类型是System.Collections.Generic.IEnumerable&lt;T&gt;。添加该命名空间的导入,并且应该可以编译。

    【讨论】:

    • 好东西!此外,如果您使用 Resharper 之类的工具,它会建议您进行此修复。
    • 嗨 Joachim,我试过“使用 System.Collections.Generic.IEnumerable”,但它说“找不到类型或命名空间名称‘T’。”有什么想法吗?
    • @Michimcchicken T 只是声明中真实类型的占位符。例如,如果你想要一个字符串的 IEnumerable,你应该只使用 IEnumerable&lt;string&gt;、整数的 IEnumerable、IEnumerable&lt;int&gt; 等。
    【解决方案2】:

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

    using System.Collections.Generic;
    

    【讨论】:

    • @GertArnold 我是 .NET 的新手。所以对我来说,接受的答案中的 using 语句应该是什么并不明显。
    猜你喜欢
    • 2014-04-20
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多