我如何用linq来转动这个物体

本文关键字:何用 linq | 更新日期: 2023-09-27 18:17:22

如果我有以下对象。

public class CFS
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public IList<Topic> Topics { get; set; }
    public IList<Status> Status { get; set; }
}
public class Topic
{
    public int ID { get; set; }
    public string Name { get; set; }
}
public class Status
{
    public int ID { get; set; }
    public string Name { get; set; }
}

如何将其放入下面的对象中,其中Topic。ID ==状态。ID,,的地位。Name = "pass"?Topic和Status字符串值将是Topic。名称和状态。分别命名值。字符串列表可以是FirstName, email,等等,这些都不重要。我意识到Topic和Status暴露相同的属性,但这只是为了这个例子。

public class SelectedTopic
{
    public string Topic { get; set; }
    public string Status { get; set; }
    public IList<string> Person { get; set; }
}

我已经尝试了几种组合的SelectMany, Any, Join,我似乎不能pivot数据的方式我想。

我如何用linq来转动这个物体

我不知道你为什么要这样做,但这里是如何:

void Main()
{
  List<Topic> topicA = new List<Topic>() { new Topic() { ID = 1, Name = "1" }, new Topic() {ID = 2 , Name = "2"}, new Topic() {ID = 3, Name = "3" } };
  List<Topic> topicB = new List<Topic>() { new Topic() { ID = 2, Name = "2" }, new Topic() {ID = 3 , Name = "3"}, new Topic() {ID = 4, Name = "4" } };
  List<Topic> topicC = new List<Topic>() { new Topic() { ID = 1, Name = "1" } };
  List<Topic> topicD = new List<Topic>() { new Topic() {ID = 2 , Name = "2"}, new Topic() {ID = 3, Name = "3" } };
  List<Status> statusA = new List<Status>() { new Status() { ID = 1, Name = "pass" }, new Status() {ID = 2 , Name = "2"}, new Status() {ID = 3, Name = "3" } };
  List<Status> statusB = new List<Status>() { new Status() { ID = 2, Name = "2" }, new Status() {ID = 3 , Name = "pass"}, new Status() {ID = 4, Name = "pass" } };
  List<Status> statusC = new List<Status>() { new Status() { ID = 1, Name = "pass" } };
  List<Status> statusD = new List<Status>() { new Status() {ID = 2 , Name = "2"}, new Status() {ID = 3, Name = "pass" } };

  List<CFS> test = new List<CFS>() { 
                      new CFS() { FirstName = "A", LastName = "A", Email = "A@A.com", Topics = topicA, Status = statusA },
                      new CFS() { FirstName = "B", LastName = "B", Email = "B@B.com", Topics = topicB, Status = statusB },
                      new CFS() { FirstName = "C", LastName = "C", Email = "C@C.com", Topics = topicC, Status = statusC },
                      new CFS() { FirstName = "D", LastName = "D", Email = "D@D.com", Topics = topicD, Status = statusD },
  };

  var result = test.SelectMany(x => x.Topics.SelectMany((t) => x.Status, (topic,status) => new { CFS = x, T = topic, S = status }))
                    .Where(x => x.S.Name == "pass" &&  x.T.ID == x.S.ID)
                    .Select(x => new {  first = x.CFS.FirstName, status = x.S.Name, topic = x.T.Name})
                    .GroupBy(x => x.topic)
                    .Select(x => new SelectedTopic { Topic = x.Key, Status = "pass", Person = x.Select(z => z.first).Distinct().ToList() })
                    .Dump();
}

在LinqPad中测试——如果你没有使用这个工具,我建议你这样做。