C# 如何使用linq在列表中迭代查找名称和值
我正在用.net开发一个MVC项目。我有一个这样的json,它非常长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
{
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 totmdbMovie
,当您使用已返回此类型的泛型反序列化对象()
)