是否可以在 LINQ 查询中拆分值并添加 if 条件?
Posted
技术标签:
【中文标题】是否可以在 LINQ 查询中拆分值并添加 if 条件?【英文标题】:Is it possible to split a value and add an if condition in a LINQ Query? 【发布时间】:2020-11-26 13:16:20 【问题描述】:我从以下 JSON 响应开始:
"d":
"RowData": [
"GenreId": 11,
"GenreName": "Musical",
"subjecturl": "subjecturl_123",
"logourl": "logourl_1"
,
"GenreId": 12,
"GenreName": "kids",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_2"
,
"GenreId": 13,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_3"
,
"GenreId": 14,
"GenreName": "Musical",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_4"
,
"GenreId": 15,
"GenreName": "Music",
"subjecturl": "subjecturl_123",
"logourl": "logourl_5"
,
"GenreId": 16,
"GenreName": "Faimaly",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_6"
,
"GenreId": 17,
"GenreName": "other",
"subjecturl": "subjecturl_abc",
"logourl": "logourl_7"
,
"GenreId": 18,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_8"
,
"GenreId": 19,
"GenreName": "kids",
"subjecturl": "subjecturl_123",
"logourl": "logourl_9"
,
"GenreId": 20,
"GenreName": "Musical",
"subjecturl": "subjecturl_123",
"logourl": "logourl_10"
,
"GenreId": 21,
"GenreName": "other",
"subjecturl": "subjecturl_123",
"logourl": "logourl_11"
]
我的目标是将上面的响应转换成如下所示的新响应:
"rows": [
"title": "Musical",
"items": [
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_1",
"subjecturlsplit": "1"
,
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_4",
"subjecturlsplit": "abc"
,
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_10",
"subjecturlsplit": "10"
]
,
"title": "kids",
"items": [
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_2",
"subjecturlsplit": "abc"
,
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_9",
"subjecturlsplit": "9"
]
,
"title": "Music",
"items": [
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_5",
"subjecturlsplit": "5"
]
,
"title": "other",
"items": [
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_3",
"subjecturlsplit": "3"
,
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_7",
"subjecturlsplit": "abc"
,
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_8",
"subjecturlsplit": "8"
,
"hdsubjecturl": "logourl_123",
"hdlogourl": "logourl_11",
"subjecturlsplit": "11"
]
,
"title": "Faimaly",
"items": [
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_6",
"subjecturlsplit": "abc"
]
]
我尝试了以下 LINQ 查询来做出上述响应:
var data = json["d"]?["RowData"]
.GroupBy(x => x["GenreName"])
.ToDictionary(g => g.Key, g => g.ToList())
.Select(kvp => new
title = kvp.Key,
items = kvp.Value.Select(x => new hdsubjecturl = x["subjecturl"], url = x["logourl"], subjecturlsplit = "" )
);
var result = JObject.FromObject(new rows = data );
使用此查询,我做出了如下回复。
"rows": [
"title": "Musical",
"items": [
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_1",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_4",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_10",
"subjecturlsplit": ""
]
,
"title": "kids",
"items": [
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_2",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_9",
"subjecturlsplit": ""
]
,
"title": "Music",
"items": [
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_5",
"subjecturlsplit": ""
]
,
"title": "other",
"items": [
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_3",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_7",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_8",
"subjecturlsplit": ""
,
"hdsubjecturl": "subjecturl_123",
"hdlogourl": "logourl_11",
"subjecturlsplit": ""
]
,
"title": "Faimaly",
"items": [
"hdsubjecturl": "subjecturl_abc",
"hdlogourl": "logourl_6",
"subjecturlsplit": ""
]
]
请注意,每个项目的subjecturlsplit
都是空的。在这里,是否可以在此 LINQ 查询中添加条件逻辑?我想将hdsubjecturl
值拆分为下划线(_
)并检查第二部分是否等于abc
。如果是,那么我想将subjecturlsplit
设置为hdsubjecturl
字段。否则,我想将hdlogourl
值拆分为下划线并将subjecturlsplit
设置为该拆分的第二部分。还是有其他方法可以做到这一点?
【问题讨论】:
听起来很像,但您的问题措辞复杂。如果您只是询问如何在表达式中编写条件逻辑,您可以使用三元运算符:condition ? resultWhenTrue: resultWhenFalse
。此外,由于这是 LINQ to Objects,因此您的选择 lambda 可以具有块体。基本上,你可以做任何事情。
【参考方案1】:
是的,您可以将块体放入 lambda 表达式中,这样您就可以在那里执行您的逻辑:
var data = json["d"]?["RowData"]
.GroupBy(x => x["GenreName"])
.ToDictionary(g => g.Key, g => g.ToList())
.Select(kvp => new
title = kvp.Key,
items = kvp.Value.Select(x =>
var hdsubjecturl = (string)x["subjecturl"];
var url = (string)x["logourl"];
var subjecturlsplit = hdsubjecturl.Split('_')[1] == "abc" ? "abc" : url.Split('_')[1];
return new hdsubjecturl, url, subjecturlsplit ;
)
);
小提琴:https://dotnetfiddle.net/RH60VC
【讨论】:
感谢您的回答。这对我很有帮助。 是否可以使用相同的查询找到唯一值。这意味着如果 logourl 相同,则仅在 JSON 中存储唯一的 logo url。我在每个地方都尝试过 .Distinct 但没有运气以上是关于是否可以在 LINQ 查询中拆分值并添加 if 条件?的主要内容,如果未能解决你的问题,请参考以下文章