0

我正在尝试填充一个 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.
4

3 回答 3

4

当然你可以做到这一点,使用 [string.EndsWith] 方法,如:1

.Where(r=> r.Name.EndsWith("00"))

将您的查询修改为:

 var objectives = objectiveData
                    .Where(r => r.Name.EndsWith("00"))
                    .Select(o => new Objective {
                                               Name = o.Name,
                                               Text = o.Text}
 );
于 2013-08-05T09:40:51.323 回答
0

有点不清楚你在问什么,但Select()如果你需要的话,你可以把复杂的逻辑放在里面:

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
            {
                ObjectiveDetailId = o.ObjectiveId,
                Name = o.Name,
                Text = o.Text,
                Objective = result
            }
        );
    }

    return result;
});

(请注意,我猜测您需要什么;您需要更正逻辑以执行您真正想要的操作。)

于 2013-08-05T09:53:28.167 回答
0

看起来你想做某种条件映射。我喜欢Matthew Watson 的回答,但有点不清楚为什么他总是Objective每次都创建一个实例。这是一些我认为更具可读性的无 LINQ 代码,并且映射了我认为您想要的方式:

public class Mapper
{
  public List<Objective> Objectives = new List<Objective>();

  public class Objective
  {
    public int ObjectiveId { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }
    public ICollection<ObjectiveDetail> ObjectiveDetails { get; set; }

    public Objective()
    {
        ObjectiveDetails = new List<ObjectiveDetail>();
    }
  }
  public class ObjectiveDetail
  {
    public int ObjectiveDetailId { get; set; }
    public int ObjectiveId { get; set; }
    public string Text { get; set; }
    public virtual Objective Objective { get; set; }
  }

  public void Assign()
  {
    var objectiveData = new[] // Hard-coded test data. We don't know what the type of each item in this list is, so I use an anonymous type
    {
      new {Name = "0600", Text = "Header 06"},
      new {Name = "0601", Text = "06 Detail 01"},
      new {Name = "0602", Text = "06 Detail 02"},
      new {Name = "0603", Text = "06 Detail 03"},
      new {Name = "0700", Text = "Header 07"},
      new {Name = "0701", Text = "07 Detail 01"},
      new {Name = "0702", Text = "07 Detail 02"}
    };

    // Create Objectives first
    var id = 1;
    foreach (var item in objectiveData.Where(i => i.Name.EndsWith("00")))
    {
      Objectives.Add(new Objective { ObjectiveId = id, Name = item.Name, Text = item.Text });
      id++;
    }

    // Create ObjectiveDetails
    id = 1;
    foreach (var item in objectiveData.Where(i => !i.Name.EndsWith("00")))
    {
      var itemLocal = item;
      var matchingObjective = Objectives.FirstOrDefault(o => o.Name.StartsWith(itemLocal.Name.Substring(0, 2)));
      var objectiveDetail = new ObjectiveDetail
      {
          ObjectiveDetailId = id,
          Text = item.Text,
          ObjectiveId = matchingObjective != null ? matchingObjective.ObjectiveId : 0,
          Objective = matchingObjective
      };

      if (matchingObjective != null)
      {
          matchingObjective.ObjectiveDetails.Add(objectiveDetail);
      }

      id++;
    }
    // At the end of this method you should have a list of Objectives, each with their ObjectiveDetails children
  }
}

输出:

在此处输入图像描述

希望这可以帮助。

于 2013-08-05T10:56:44.497 回答