Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ-选择一个类列表作为类成员的字段_C#_Linq - Fatal编程技术网

C# LINQ-选择一个类列表作为类成员的字段

C# LINQ-选择一个类列表作为类成员的字段,c#,linq,C#,Linq,例如,我有以下课程: public class Person{ public string name, surname; public int age; public List<Aspect> aspect; } public class Aspect { public string key; public string value; } 换句话说,LINQ表达式返回一个对象列表,如: [0]{name="exampleName1", s

例如,我有以下课程:

public class Person{  
  public string name, surname;  
  public int age;  
  public List<Aspect> aspect; 
}

public class Aspect
{
    public string key;
    public string value;
}
换句话说,LINQ表达式返回一个对象列表,如:

[0]{name="exampleName1", surname="surname1", age="age1", aspectKey1 = "aspectValue11", aspectKey2 = "aspectValue2", ...}
[1]{name="exampleName2", surname="surname2", age="age2", aspectKey1 = "aspectValue12", aspectKey2 = "aspectValue22", ...}

使用LINQ select可以做到这一点吗?

您可以使用以下方法:

string ReportPerson(Person person)
{
    return string.Format("{0}, {1}, {2}", person.name, person.surname,
        string.Join(", ", person.aspect.SelectMany(a => new[] {a.key, a.value})));
}

编辑以响应您的编辑:

这是不可能的,因为匿名类型是在编译时定义的。构建一系列这样的属性的LINQ查询需要知道编译时存在多少键和值,以便将数据投影到适当的类型中

另一种选择可能是将数据放入
字典
,这样可以为每个选项设置一个条目:

Dictionary<string,string> ProjectPerson(Person person)
{
    var results = Dictionary<string,string>();
    results.Add("Name", person.name);
    results.Add("Surname", person.surname);
    for (int i=0;i<person.aspect.Count;++i)
    {
         results.Add("aspectKey" + i.ToString(), person.aspect[i].key);             
         results.Add("aspectValue" + i.ToString(), person.aspect[i].value);
    }

    return results;
}
而不是能够写:

string name = projectedPerson.Name;
dynamic projected = ProjectPerson(somePerson);

Console.WriteLine(projected.Name);
Console.WriteLine(projected.aspectKey3); // Assuming there are at least 4 aspects in the person
如果您确实想使用最后一个选项,使用
dynamic
可以:

dynamic ProjectPerson(Person person)
{
    dynamic result = new ExpandoObject();
    var results = result as IDictionary<string, object>();
    results.Add("Name", person.name);
    results.Add("Surname", person.surname);
    for (int i=0;i<person.aspect.Count;++i)
    {
         results.Add("aspectKey" + i.ToString(), person.aspect[i].key);             
         results.Add("aspectValue" + i.ToString(), person.aspect[i].value);
    }

    return result;
}

您可以使用以下内容:

string ReportPerson(Person person)
{
    return string.Format("{0}, {1}, {2}", person.name, person.surname,
        string.Join(", ", person.aspect.SelectMany(a => new[] {a.key, a.value})));
}

编辑以响应您的编辑:

这是不可能的,因为匿名类型是在编译时定义的。构建一系列这样的属性的LINQ查询需要知道编译时存在多少键和值,以便将数据投影到适当的类型中

另一种选择可能是将数据放入
字典
,这样可以为每个选项设置一个条目:

Dictionary<string,string> ProjectPerson(Person person)
{
    var results = Dictionary<string,string>();
    results.Add("Name", person.name);
    results.Add("Surname", person.surname);
    for (int i=0;i<person.aspect.Count;++i)
    {
         results.Add("aspectKey" + i.ToString(), person.aspect[i].key);             
         results.Add("aspectValue" + i.ToString(), person.aspect[i].value);
    }

    return results;
}
而不是能够写:

string name = projectedPerson.Name;
dynamic projected = ProjectPerson(somePerson);

Console.WriteLine(projected.Name);
Console.WriteLine(projected.aspectKey3); // Assuming there are at least 4 aspects in the person
如果您确实想使用最后一个选项,使用
dynamic
可以:

dynamic ProjectPerson(Person person)
{
    dynamic result = new ExpandoObject();
    var results = result as IDictionary<string, object>();
    results.Add("Name", person.name);
    results.Add("Surname", person.surname);
    for (int i=0;i<person.aspect.Count;++i)
    {
         results.Add("aspectKey" + i.ToString(), person.aspect[i].key);             
         results.Add("aspectValue" + i.ToString(), person.aspect[i].value);
    }

    return result;
}

旁注:我建议使用属性而不是公共字段,以及标准的.NET命名准则(“名称”、“姓氏”、“方面”等)旁注:我建议使用属性而不是公共字段,以及标准的.NET命名准则(“名称”、“姓氏”、“方面”等)我的错,我没有正确解释,我已经添加了有关该要求的更多信息。Thanks@fnsanchez这在C#中是不可能的-提供了一个替代方案。我的错是,我没有正确解释,我添加了更多关于需求的信息。Thanks@fnsanchez这在C#中是不可能的-提供了一种选择。