如何遍历答案?

时间:2019-07-12 08:47:24

标签: c# linq

我有这些动物:

public class AnimalAnswer
{
    public string QuestionId
    {
        get;
        set;
    }

    public int Value
    {
        get;
        set;
    }
}


void Main()
{
    List<AnimalAnswer> animals = new List<AnimalAnswer>();
    animals.Add(new AnimalAnswer() { Value = 3, QuestionId = "animals[0].howbig" });
    animals.Add(new AnimalAnswer() { Value = 5, QuestionId = "animals[0].howold" });

    animals.Add(new AnimalAnswer() { Value = 9, QuestionId = "animals[1].howbig" });
    animals.Add(new AnimalAnswer() { Value = 2, QuestionId = "animals[1].howold" });

    animals.Add(new AnimalAnswer() { Value = 10, QuestionId = 
        "vacation.howmanyvacationdays" });

    Regex animalsRegEx = new Regex(@"animals\[.*?\]");

    // This gives me the array elements
    animals.Where(x => animalsRegEx.IsMatch(x.QuestionId)).Select(o => 
        o.QuestionId);
}

我无法弄清两件事-

问题1

  

我如何遍历这组动物答案并检查如何   又大又多大了?棘手的部分是有些数组   元素,有些则没有。

问题2

  

我怎么知道这个动物阵列有多大?

3 个答案:

答案 0 :(得分:0)

使用GroupBy:

namespace ConsoleApplication120
{
    class Program
    {
        static void Main(string[] args)
        {
            List<AnimalAsnwer> animals = new List<AnimalAsnwer>() {
                new AnimalAsnwer() { index = 1, Value = 3, QuestionId = "howbig"},
                new AnimalAsnwer() { index = 2, Value = 5, QuestionId = "howold"},
                new AnimalAsnwer() { index = 3, Value = 9, QuestionId = "howbig" },
                new AnimalAsnwer() { index = 4, Value = 2, QuestionId = "howold" },
                new AnimalAsnwer() { index = 5, Value = 10, QuestionId = "howmanyvacationdays" }
            };

            var summary = animals.GroupBy(x => x.QuestionId)
                .Select(x => new { question = x.Key, values = x.Select(y => new { value = y.Value, index = y.index }).ToList() }).ToList();
        }
    }
    public class AnimalAsnwer
    {
        public string QuestionId { get; set; }
        public int Value { get; set; }
        public int index { get; set; }
    } 
}

答案 1 :(得分:0)

您可以使用GroupBy()和ToDictionnary()来做到这一点:

var questionIdRegex = new Regex(@"^(.*)\.(.*$)");

Dictionary<string, Dictionary<string, int>> dicElementQuestion = animals.GroupBy(x => questionIdRegex.Match(x.QuestionId).Groups[1].Value)
            .ToDictionary(x => x.Key,
                          x => x.ToDictionary(y => questionIdRegex.Match(y.QuestionId).Groups[2].Value,
                                              y => y.Value));

答案 2 :(得分:0)

您可能应该重新考虑您的解决方案和方法。但是以下内容将满足您的要求(带有解释代码的内联注释):

class Program
{
    static void Main(string[] args)
    {
        // Dictionary to hold the animal by id (id is assumed to be the index in the original array.
        var result = new Dictionary<int, Animal>();

        foreach (var answer in animals)
        {
            // Parse the value between the "[" and "]" if the question id contains the word "animals" into an int for the dictionary key.
            if (answer.QuestionId.Contains("animals") && int.TryParse(answer.QuestionId.Substring(answer.QuestionId.IndexOf('[') + 1, 1), out var id))
            {
                if (!result.TryGetValue(id, out var value))
                {
                    value = new Animal();
                    result[id] = value;
                }

                // Check if the answer is to "howold" o "howbig" and construct the animal object.
                if (answer.QuestionId.Contains("howold"))
                {
                    value.Age = answer.Value;
                }
                else if (answer.QuestionId.Contains("howbig"))
                {
                    value.Size = answer.Value;
                }
                else
                {
                    Debug.Assert(false, "Unidentified animal answer");
                }
             }
          }

          // Number of animals in the array:
          var animalCount = result.Keys.Count;
       }
    }

    // Define a class for animal information.
    public class Animal
    {
        public int Id { get; set; }

        public int Age { get; set; }

        public int Size { get; set; }
    }
 }