LINQ - 类型参数不能从select中的用法推断出来

9 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();
    }
Run Code Online (Sandbox Code Playgroud)

我在"选择"一行中收到错误说:

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.
Run Code Online (Sandbox Code Playgroud)

这是我的Objective类:

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; }
}
Run Code Online (Sandbox Code Playgroud)

Ply*_*223 15

你需要

return result;
Run Code Online (Sandbox Code Playgroud)

在你的表达结束时.


Kin*_*ing 2

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
                }
            );
        }
        //you miss this
        return result;
    });
Run Code Online (Sandbox Code Playgroud)