Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.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#_Asp.net_Asp.net Mvc_Linq - Fatal编程技术网

C# 如何使用linq在列表中迭代查找名称和值

C# 如何使用linq在列表中迭代查找名称和值,c#,asp.net,asp.net-mvc,linq,C#,Asp.net,Asp.net Mvc,Linq,我正在用.net开发一个MVC项目。我有一个这样的json,它非常长 { credit_id: "57214f73c3a3681e800005f4", department: "Production", id: 84493, job: "Casting", name: "Mickie McGowan", profile_path: "/k7TjJBfINsg8vLQxJwos6XObAD6.jpg" }, { credit_id: "572

我正在用.net开发一个MVC项目。我有一个这样的json,它非常长

{
    credit_id: "57214f73c3a3681e800005f4",
    department: "Production",
    id: 84493,
    job: "Casting",
    name: "Mickie McGowan",
    profile_path: "/k7TjJBfINsg8vLQxJwos6XObAD6.jpg"
},
{
    credit_id: "572294979251413eac0007e2",
    department: "Art",
    id: 7927,
    job: "Art Direction",
    name: "Ricky Nierva",
    profile_path: null
},
{
    credit_id: "572297cdc3a3682d240008f9",
    department: "Visual Effects",
    id: 7894,
    job: "Visual Effects",
    name: "Jean-Claude Kalache",
    profile_path: null
},
我把jason放在一个名为“directorsData.crew”的列表中,我必须找到他们的部门正在“指导”的名称,并将其添加到一个董事列表中。我怎么能和LINQ一起做呢

像这样

var directorsData = (tmdbMovie)JsonConvert.DeserializeObject<tmdbMovie>(findDirectors);

List<string> directors = new List<string>();
if (directorsData != null)
{
    if (directorsData.crew.Count != 0)
    {
        foreach (var item in directorsData.crew) 
        {
            var name = directorsData.crew. ===?
            directors.Add(name);
        }
    }
}
var directorsData=(tmdbMovie)JsonConvert.DeserializeObject(findDirectors);
列表控制器=新列表();
if(directorsData!=null)
{
如果(directorsData.crew.Count!=0)
{
foreach(directorsData.crew中的var项)
{
变量名称=directorsData.crew.==?
董事。增加(姓名);
}
}
}

使用Linq,您可以获得所有姓名,如列表所示,该部门是
主管的

var nameList = directorsData.crew.Where(a =>a.department == "Directing")
                                 .Select(a => a.name).ToList();
此外,还可以在反序列化中删除类型转换

var directorsData = JsonConvert.DeserializeObject<tmdbMovie>(findDirectors);
var directorsData=JsonConvert.DeserializeObject(findDirectors);

使用Linq,您可以获得所有姓名,如列表所示,该部门是
主管的

var nameList = directorsData.crew.Where(a =>a.department == "Directing")
                                 .Select(a => a.name).ToList();
此外,还可以在反序列化中删除类型转换

var directorsData = JsonConvert.DeserializeObject<tmdbMovie>(findDirectors);
var directorsData=JsonConvert.DeserializeObject(findDirectors);

这将返回一组
tmdbMovie
类,这些类的department等于“Directing”

如果只需要名称,在字符串集合中,可以执行以下操作:

var nameCollection = directorsData.crew.Where(x => x.Department.Equals("Directing"))
                                       .Select(x => x.Name).ToList();
nameCollection.ForEach(x =>
{
    //X will be the string value of the name
    //Your code here
});
如果要循环浏览所有名称,可以执行以下操作:

var nameCollection = directorsData.crew.Where(x => x.Department.Equals("Directing"))
                                       .Select(x => x.Name).ToList();
nameCollection.ForEach(x =>
{
    //X will be the string value of the name
    //Your code here
});

这将返回一组
tmdbMovie
类,这些类的department等于“Directing”

如果只需要名称,在字符串集合中,可以执行以下操作:

var nameCollection = directorsData.crew.Where(x => x.Department.Equals("Directing"))
                                       .Select(x => x.Name).ToList();
nameCollection.ForEach(x =>
{
    //X will be the string value of the name
    //Your code here
});
如果要循环浏览所有名称,可以执行以下操作:

var nameCollection = directorsData.crew.Where(x => x.Department.Equals("Directing"))
                                       .Select(x => x.Name).ToList();
nameCollection.ForEach(x =>
{
    //X will be the string value of the name
    //Your code here
});

那么我想我不需要foreach了?那么我想我不需要foreach了?当你反序列化时,你不需要键入cast back to
tmdbMovie
,因为你使用的是泛型的
DeserializeObject()
,它已经返回了这个类型:)当你反序列化时,你不需要键入cast back to
tmdbMovie
,当您使用已返回此类型的泛型
反序列化对象()