C# 获取一个包含所有特定元素的标志
若元素包含带有标志的特定元素,则获取该元素并将其添加到集合中。在我的代码中,这意味着 获取所有歌曲,其中genreid==18/,例如,数字可以是任何int32/。我很高兴看到任何答案。我用linq得到它,但我只看到那个数字是int32,其他什么都没有。 这是我的代码:(歌曲列表是列表)C# 获取一个包含所有特定元素的标志,c#,arrays,linq,collections,ienumerable,C#,Arrays,Linq,Collections,Ienumerable,若元素包含带有标志的特定元素,则获取该元素并将其添加到集合中。在我的代码中,这意味着 获取所有歌曲,其中genreid==18/,例如,数字可以是任何int32/。我很高兴看到任何答案。我用linq得到它,但我只看到那个数字是int32,其他什么都没有。 这是我的代码:(歌曲列表是列表) var json=e.Result; var jobject=jobject.Parse(json); var serializer=new JsonSerializer(); serializer.conve
var json=e.Result;
var jobject=jobject.Parse(json);
var serializer=new JsonSerializer();
serializer.converts.Add(新的ResponseDataConverter());
var songList=jobject[“response”].ToObject(序列化程序);
song.GenreId=(int)来自歌曲列表中的other。other.GenreId==18的歌曲选择other.GenreId;
如果它对您有帮助,这是我的反序列化类:(使用重写的方法)
公共抽象类响应数据
{
[JsonProperty(PropertyName=“id”)]
公共int Id{get;set;}
}
公共类数据:ResponseData
{
[JsonProperty(PropertyName=“artist”)]
公共字符串艺术家{get;set;}
[JsonProperty(PropertyName=“title”)]
公共字符串SongName{get;set;}
[JsonProperty(PropertyName=“url”)]
公共字符串SongUri{get;set;}
[JsonProperty(PropertyName=“duration”)]
公共整数持续时间{get;set;}
[JsonProperty(PropertyName=“owner\u id”)]
public int OwnerId{get;set;}
[JsonProperty(PropertyName=“歌词id”)]
public int-LyricsId{get;set;}
[JsonProperty(PropertyName=“genre_id”)]
public int GenreId{get;set;}
}
公共类UserData:ResponseData
{
[JsonProperty(PropertyName=“photo”)]
公共字符串Photo{get;set;}
[JsonProperty(PropertyName=“name”)]
公共字符串名称{get;set;}
[JsonProperty(PropertyName=“name\u gen”)]
公共字符串NameGen{get;set;}
}
公共类歌曲列表
{
[JsonProperty(PropertyName=“count”)]
公共整数计数{get;set;}
[JsonIgnore]
公共列表歌曲{get;set;}
[JsonIgnore]
公共列表用户{get;set;}
[JsonProperty(PropertyName=“items”)]
公众回应数据[]项
{
得到
{
return(用户??Enumerable.Empty()).Cast().Concat((歌曲??Enumerable.Empty()).Cast()).ToArray();
}
设置
{
Songs=(值??Enumerable.Empty())。of type().ToList();
Users=(值??Enumerable.Empty()).OfType().ToList();
}
}
}
公共类响应的转换器:JsonConverter
{
公共覆盖布尔CanConvert(类型objectType)
{
返回类型(ResponseData).IsAssignableFrom(objectType);
}
公共重写对象ReadJson(JsonReader,
类型objectType、对象existingValue、JsonSerializer序列化程序)
{
JObject item=JObject.Load(读卡器);
如果(项目[“名称”]!=null)
{
返回item.ToObject();
}
其他的
{
返回item.ToObject();
}
}
public override void WriteJson(JsonWriter writer,
对象值,JsonSerializer序列化程序)
{
抛出新的NotImplementedException();
}
}
我需要你的帮助。我花了半天的时间研究那个问题。使用IEnumerable collection很难。太难了。也许我错过了什么,但我还年轻,只是在学习。这就是我对你的理解(如果我错了,请纠正我) 您有一个歌曲列表(在本例中为
songList.songs
),您希望获得一个IEnumerable
集合,其中包含genreid==18(或任何id)的所有歌曲
只需查看最后一行代码(linq查询)…可能类似于
var songsWithId = from sd in songList.Songs
where sd.GenreId == 18
select sd;
其中songWithId
是一个IEnumerable
在你的代码中,你只选择了类型Id(
选择其他.GenreId
),这让我觉得我误解了你的问题。这就是我对你的理解(如果我错了,请纠正我)
您有一个歌曲列表(在本例中为songList.songs
),您希望获得一个IEnumerable
集合,其中包含genreid==18(或任何id)的所有歌曲
只需查看最后一行代码(linq查询)…可能类似于
var songsWithId = from sd in songList.Songs
where sd.GenreId == 18
select sd;
其中songWithId
是一个IEnumerable
在你的代码中,你只选择了类型Id(
选择其他.GenreId
),这让我觉得我误解了你的问题。这就是我对你的理解(如果我错了,请纠正我)
您有一个歌曲列表(在本例中为songList.songs
),您希望获得一个IEnumerable
集合,其中包含genreid==18(或任何id)的所有歌曲
只需查看最后一行代码(linq查询)…可能类似于
var songsWithId = from sd in songList.Songs
where sd.GenreId == 18
select sd;
其中songWithId
是一个IEnumerable
在你的代码中,你只选择了类型Id(
选择其他.GenreId
),这让我觉得我误解了你的问题。这就是我对你的理解(如果我错了,请纠正我)
您有一个歌曲列表(在本例中为songList.songs
),您希望获得一个IEnumerable
集合,其中包含genreid==18(或任何id)的所有歌曲
只需查看最后一行代码(linq查询)…可能类似于
var songsWithId = from sd in songList.Songs
where sd.GenreId == 18
select sd;
其中songWithId
是一个IEnumerable
在你的代码中,你只选择了类型Id(选择other.GenreId
),这让我觉得我误解了你的问题。我不知道你为什么要这样做
List<int> SongsWithGenreId18 = (from other in songList.Songs
where other.GenreId == 18
select other.GenreId).ToList();
var songsWithSpecificGenreId = songList.Where(song => song.GenreId == 18);