gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:40:19 24 4
gpt4 key购买 nike

我有以下 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com