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
使用LINQ将所有元素连接到字符串中_Linq_Entity Framework - Fatal编程技术网

使用LINQ将所有元素连接到字符串中

使用LINQ将所有元素连接到字符串中,linq,entity-framework,Linq,Entity Framework,我有以下课程: public class People { public int ID { get; set; } public List<Right> Rights { get; set; } } public class Right { public int ID { get; set; } public int Val { get; set; } } 公共阶层人士 { 公共int ID{get;set;} 公共列表权限{get;set;}

我有以下课程:

public class People 
{
    public int ID { get; set; }
    public List<Right> Rights { get; set; }
}

public class Right
{
    public int ID { get; set; }
    public int Val { get; set; }
}
公共阶层人士
{
公共int ID{get;set;}
公共列表权限{get;set;}
}
公共阶级权利
{
公共int ID{get;set;}
公共int Val{get;set;}
}
具有以下值:

  • 联系人:ID:1
    • 对:
      • 身份证号码:1
      • 瓦尔:5
    • 对:
      • 身份证号码:2
      • 瓦尔:4
我想检索人民的所有
Val
权利(在单个字符串中)


因此,对于ID为1:getting
“5,4”

的用户,可以使用
SelectMany来展平结构,例如:

var vals = people.Where(p => p.ID == 1)
                 .SelectMany(p => p.Rights.Select(r => Val));

var str = String.Join(",", vals.Select(v => v.ToString());
List=。。。;
string result=string.Join(“,”,(people.First(p=>p.ID==1)).Rights.Select(r=>r.Val));
例如:

        List<People> people = new List<People>()
        {
            new People() 
            {
                ID = 1, Rights = new List<Right>()
                {
                    new Right() { ID = 1, Val = 5 }, 
                    new Right() { ID = 2, Val = 10 }, 
                }
            },

            new People() 
            {
                ID = 2, Rights = new List<Right>()
                {
                    new Right() { ID = 1, Val = 6 }, 
                    new Right() { ID = 2, Val = 11 }, 
                }
            }
        };

        string result = string.Join(",", (people.First(p => p.ID == 1)).Rights.Select(r => r.Val));

        Console.WriteLine(result);
List people=新列表()
{
新人()
{
ID=1,权限=新列表()
{
新的Right(){ID=1,Val=5},
新的Right(){ID=2,Val=10},
}
},
新人()
{
ID=2,权限=新列表()
{
新的Right(){ID=1,Val=6},
新的Right(){ID=2,Val=11},
}
}
};
string result=string.Join(“,”,(people.First(p=>p.ID==1)).Rights.Select(r=>r.Val));
控制台写入线(结果);

输出:
5,10

您的代码被破坏:
public List{get;set;}
听起来像是Where、Select和String的混合体。Join应该对您有用。我更新了以更正它。@Jon您当然是对的,但我没有成功。@Bronzato:这表明您尝试了一些东西,您应该在问题中包括这些。
        List<People> people = new List<People>()
        {
            new People() 
            {
                ID = 1, Rights = new List<Right>()
                {
                    new Right() { ID = 1, Val = 5 }, 
                    new Right() { ID = 2, Val = 10 }, 
                }
            },

            new People() 
            {
                ID = 2, Rights = new List<Right>()
                {
                    new Right() { ID = 1, Val = 6 }, 
                    new Right() { ID = 2, Val = 11 }, 
                }
            }
        };

        string result = string.Join(",", (people.First(p => p.ID == 1)).Rights.Select(r => r.Val));

        Console.WriteLine(result);