如何通过linq查询读取所有代码并保存到单个集合中



我想在一个集合中的单个linq查询中读取RoleAmpLabelAdded中的所有AuthRole代码。我正在写下面的查询来提取数据,然后当前进行字符串连接。请给我推荐一些有效的解决方案

string strRoleCodesA = string.Join(",", request.ApplicationsAccess.CategoryAccessType
.SelectMany(c => c.BasicApplications.RoleAmpLabelAdded
.Select(t => t.AuthRoleCode)));
string  strRoleCodesB = string.Join(",", request.ApplicationsAccess.CategoryAccessType
.SelectMany(c => c.RegualrApplications.RoleAmpLabelAdded
.Select(t => t.AuthRoleCode)));
string strRoleCodesC = string.Join(",", request.ApplicationsAccess.CategoryAccessType
.SelectMany(c => c.OptionalSpecialApplications.RoleAmpLabelAdded
.Select(t => t.AuthRoleCode)));
strRoleCodesD = string.Concat(strRoleCodesA, strRoleCodesB, strRoleCodesC);

我不想加入上面的字符串。下面是上面代码的类结构。

public partial class ApplicationsAccess
{
[JsonProperty("categoryAccessType")]
public List<CategoryAccessTypeAMP> CategoryAccessType { get; set; }
}
public partial class CategoryAccessTypeAMP
{
[JsonProperty("categoryCode")]
public string CategoryCode { get; set; }
[JsonProperty("basicApplications")]
public Applications BasicApplications { get; set; }
[JsonProperty("regualrApplications")]
public Applications RegualrApplications { get; set; }
[JsonProperty("optionalSpecialApplications")]
public Applications OptionalSpecialApplications { get; set; }
}
public partial class Applications
{
[JsonProperty("applictaionType")]
public string ApplictaionType { get; set; }
[JsonProperty("roleAmpLabelAdded")]
public List<RoleAmpLabels> RoleAmpLabelAdded { get; set; }
[JsonProperty("roleAmpLabelRemoved")]
public List<RoleAmpLabels> RoleAmpLabelRemoved { get; set; }
}
public partial class RoleAmpLabels
{
[JsonProperty("roleAMPLabel")]
public string RoleAmpLabelRoleAmpLabel { get; set; }
[JsonProperty("authRoleCode")]
public string AuthRoleCode { get; set; }
[JsonProperty("authRoleName")]
public string AuthRoleName { get; set; }
[JsonProperty("applications")]
public List<Applications> Applications { get; set; }
}
public partial class Applications
{
[JsonProperty("appId")]
public string AppId { get; set; }
[JsonProperty("appCode")]
public string AppCode { get; set; }
[JsonProperty("appName")]
public string AppName { get; set; }
}

类是从以下JSON字符串加载的:

{
"applicationsAccess": {
"categoryAccessType": [
{
"categoryCode": "string",
"basicApplications": {
"applictaionType": "string",
"roleAmpLabelAdded": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
],
"roleAmpLabelRemoved": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
]
},
"regualrApplications": {
"applictaionType": "string",
"roleAmpLabelAdded": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
],
"roleAmpLabelRemoved": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
]
},
"optionalSpecialApplications": {
"applictaionType": "optionalSpecialApplications",
"roleAmpLabelAdded": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
],
"roleAmpLabelRemoved": [
{
"roleAMPLabel": "string",
"authRoleCode": "string",
"authRoleName": "string",
"applications": [
{
"appId": "string",
"appCode": "string",
"appName": "string"
}
]
}
]
}
}
]
}
}

所以如果你这样做:

request.ApplicationsAccess.CategoryAccessType
.SelectMany(c => new[] { c.BasicApplications, c.RegualrApplications, c.OptionalSpecialApplications})

SelectMany将使";应用程序阵列";合并到所有应用程序的单个列表中,以及列表的子列表

然后你加上这个:

.SelectMany(a => a.RoleAmpLabelAdded)

它可以为您获取所有应用程序中所有RoleAmpLabels列表中的所有RoleAmpLabel,并将其扁平化为一个RoleAmpLabel列表,因此您可以执行以下操作:

.Select(r => r.AuthRoleCode)

哪个是可以直接馈送到string.JoinIEnumerable<string>.Select(t=>t.AuthRoleCode((;


如果您将类名更改为单数的C#约定,并将属性名更改为复数(如果它们是一个集合(,则会更容易推理。

List<RoleAmpLabels> RoleAmpLabelAdded

最好写成以下之一:

List<RoleAmpLabel> RoleAmpLabelsAdded
List<RoleAmpLabel> AddedRoleAmpLabels //my preference

如果您创建了一个集合类,其唯一目的是成为集合,则追加";集合";与DataRowCollection一样。如果你有一个正则类,恰好有其他类的倍数,那么将该属性命名为复数

Dealership{
List<Car> CarsForSale
List<Car> CarsInForRepair
}

由于类来自JSON字符串,因此可以使用JObject.Parse解析整个字符串,并使用JSON Path检索所有AuthRoleCode元素,例如:

var o=JObject.Parse(json);
var codes=o.SelectTokens("$..authRoleCode").Select(s=>(string)s).ToArray();

需要强制转换来提取SelectToken返回的JToken对象的值。

这可以用String.Join()转换为逗号分隔的列表,例如;

var codes=o.SelectTokens("$..authRoleCode");
var csv=String.Join(",",codes);

在这种情况下不需要强制转换,因为String.Join调用每个元素的ToString()方法。

使用问题的JSON运行此代码返回:

string,string,string,string,string,string

最新更新