我正在尝试填充一个 Objective 和 ObjectiveDetail 对象。这是我的课程:
partial class Objective
{
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; }
}
public partial class ObjectiveDetail
{
public int ObjectiveDetailId { get; set; }
public int ObjectiveId { get; set; }
public string Text { get; set; }
public virtual Objective Objective { get; set; }
}
我目前正在从此调用中填充唯一的 Objective 对象:
var objectiveData = GetContent.GetType5();
var objectives = objectiveData.Select(o => new Objective {
Name = o.Name,
Text = o.Text}
);
数据如下所示:
Name Text
0600 header 1
0601 detail abc
0602 detail def
0603 detail ghi
0700 header 2
0701 detail xyz
有没有办法可以修改我的 LINQ,以便只有name
字段内容以“00”结尾的数据进入目标对象(就像现在一样),当name
字段内容以“01”结尾的数据时,它会创建一个带有“detail abc”等的新ObjectiveDetail对象进入该text
字段。
这是最终结果应该是什么样子的图片:
A collection of Objectives
new Objective { name = "header 1",
ObjectiveDetails = A collection of ObjectiveDetails
name = "detail abc"
name = "detail def" etc.