如何使用linq旋转此对象?

时间:2014-08-08 17:54:31

标签: c# linq

如果我有以下物品。

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 == Status.ID&amp;&amp ;; Status.Name =&#34;传递&#34;? Topic和Status字符串值分别是Topic.Name和Status.Name值。字符串列表可以是FirstName,电子邮件,无论如何,该部分是微不足道的。我意识到主题和状态暴露了相同的属性,但这仅仅是为了这个例子。

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

我尝试了几种SelectMany,Any,Join的组合,而我似乎无法按照我想要的方式转动数据。

1 个答案:

答案 0 :(得分:1)

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

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中测试 - 如果您不使用此工具,我建议您这样做。