c# - LINQ - 无法从 select 中的用法推断出类型参数

标签 c# linq

我有以下 objectiveData 是:IEnumerable<Objective>

    public IList<Objective> createObjectives()
    {
        var objectiveData = GetContent.GetType5(); 
        var objectives = objectiveData.Select(o => {
            var result = new Objective {
                            Name = o.Name,
                            Text = o.Text
            };
            if (o.Name != null && o.Name.EndsWith("01"))
            {
                result.ObjectiveDetails.Add
                (
                    new ObjectiveDetail
                    {
                        Text = o.Text
                    }
                );
            }
        });
        return objectives.ToList();
    }

我在带有“select”的行中收到错误消息:

The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>
(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' 
cannot be inferred from the usage. Try specifying the type arguments explicitly.

这是我的目标类:

public partial class Objective : AuditableTable
{
    public Objective()
    {
        this.ObjectiveDetails = new List<ObjectiveDetail>();
    }
    public int ObjectiveId { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public virtual ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }
}

最佳答案

你需要

return result;

在表达式的末尾。

关于c# - LINQ - 无法从 select 中的用法推断出类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18055802/

相关文章:

c# - 传递到字典中的模型项是 View 模型类型,但需要通用 IEnum

c# - LINQ 项目总和集合以返回带有结果的对象(多列)

json - 对平面列表进行分组并输出分层 JSON

c# - Xamarin 表单按钮 OnClick

c# - Response.Cache.SetCacheability 不起作用

c# - 求源码.NET图像处理 "lasso"选择器

c# - 换日更新问题

c# - 有人可以向我解释一下这个 GetCardinality 方法在做什么吗?

c# - Linq to NHibernate 返回与 HQL 不同的结果?

sql - 如何在 Linq 中编写此交叉表类型查询(提供表和数据)