如何使用 LINQ 中的键对值从单个字段中获取重复数据?
Posted
技术标签:
【中文标题】如何使用 LINQ 中的键对值从单个字段中获取重复数据?【英文标题】:How to fetch the duplicate data from single field using key pair value in LINQ? 【发布时间】:2021-01-05 16:24:36 【问题描述】:这是我下面的 JSON
"status": "OK",
"Response": [
"id": 43,
"Name": "name 1",
"SubName": "Sub 1"
"Link": "Link 1",
"Active": 1,
"Genre": 8,
"Description": "Description 1",
"Languages": "",
"IsDeleted": 0,
"GenreId": 8
,
"id": 44,
"Name": "name 1",
"SubName": "Sub 2"
"Link": "Link 2",
"Active": 1,
"Genre": 9,
"Description": "Description 2",
"Languages": "",
"IsDeleted": 0,
"GenreId": 9
,
"id": 45,
"Name": "name 1",
"SubName": "Sub 3"
"Link": "Link 3",
"Active": 1,
"Genre": 10,
"Description": "Description 3",
"Languages": "",
"IsDeleted": 0,
"GenreId": 10
,
"id": 46,
"Name": "name 2",
"SubName": ""
"Link": "Link 4",
"Active": 1,
"Genre": 11,
"Description": "Description 4",
"Languages": "",
"IsDeleted": 0,
"GenreId": 11
,
"id": 47,
"Name": "name 3",
"SubName": "Sub 1"
"Link": "Link 5",
"Active": 1,
"Genre": 12,
"Description": "Description 5",
"Languages": "",
"IsDeleted": 0,
"GenreId": 12
,
"id": 48,
"Name": "name 3",
"SubName": "Sub 2"
"Link": "Link 6",
"Active": 1,
"Genre": 13,
"Description": "Description 6",
"Languages": "",
"IsDeleted": 0,
"GenreId": 13
,
"id": 49,
"Name": "name 3",
"SubName": "Sub 3"
"Link": "Link 7",
"Active": 1,
"Genre": 14,
"Description": "Description 7",
"Languages": "",
"IsDeleted": 0,
"GenreId": 14
,
"id": 50,
"Name": "name 3",
"SubName": "Sub 4"
"Link": "Link 8",
"Active": 1,
"Genre": 15,
"Description": "Description 8",
"Languages": "",
"IsDeleted": 0,
"GenreId": 15
,
"id": 51,
"Name": "name 4",
"SubName": ""
"Link": "Link 9",
"Active": 1,
"Genre": 16,
"Description": "Description 9",
"Languages": "",
"IsDeleted": 0,
"GenreId": 16
]
我试着像下面的 JSON 那样做
name 1.json
"rows": [
"title": "Sub 1",
"items": [
"id": 43,
"Name": "name 1",
"SubName": "Sub 1"
"Link": "Link 1",
"Active": 1,
"Genre": 8,
"Description": "Description 1",
"Languages": "",
"IsDeleted": 0,
"GenreId": 8
]
,
"title": "Sub 2",
"items": [
""id": 44,
"Name": "name 1",
"SubName": "Sub 2"
"Link": "Link 2",
"Active": 1,
"Genre": 9,
"Description": "Description 2",
"Languages": "",
"IsDeleted": 0,
"GenreId": 9
]
,
"title": "Sub 3",
"items": [
""id": 45,
"Name": "name 1",
"SubName": "Sub 3"
"Link": "Link 3",
"Active": 1,
"Genre": 10
"Description": "Description 3",
"Languages": "",
"IsDeleted": 0,
"GenreId": 10
]
]
name 3.json
"rows": [
"title": "Sub 1",
"items": [
"id": 47,
"Name": "name 3",
"SubName": "Sub 1"
"Link": "Link 5",
"Active": 1,
"Genre": 12,
"Description": "Description 5",
"Languages": "",
"IsDeleted": 0,
"GenreId": 12
]
,
"title": "Sub 2",
"items": [
""id": 48,
"Name": "name 3",
"SubName": "Sub 2"
"Link": "Link 6",
"Active": 1,
"Genre": 13,
"Description": "Description 6",
"Languages": "",
"IsDeleted": 0,
"GenreId": 13
]
,
"title": "Sub 3",
"items": [
""id": 49,
"Name": "name 3",
"SubName": "Sub 3"
"Link": "Link 7",
"Active": 1,
"Genre": 14
"Description": "Description 7",
"Languages": "",
"IsDeleted": 0,
"GenreId": 14
]
,
"title": "Sub 4",
"items": [
""id": 50,
"Name": "name 3",
"SubName": "Sub 4"
"Link": "Link 8",
"Active": 1,
"Genre": 15
"Description": "Description 8",
"Languages": "",
"IsDeleted": 0,
"GenreId": 15
]
]
我正在尝试使用重复名称明智地分隔一个 JSON,如上面。我在 JSON 中只发现了重复的名称,没有其他内容。
有人知道如何获取重复值吗?
我尝试了 Linq 查询并使用了
mygeneratedresponse.Skip(1).ToList();
它正在工作。但是有更好的解决方案吗?
【问题讨论】:
我尝试使用 Linq 查询并应用 skip(1)。它正在工作。但是,这不是更好的解决方案。 - 那么请edit 你的问题表明你已经尝试过并解释为什么“这不是更好的解决方案”。来自How to Ask:帮助其他人重现问题...如果您的问题与您编写的代码有关,您应该包括一些。包含足够的代码以允许其他人重现该问题。 您有类似的问题 here 和 here 所以如果您能以最小的方式重现您的问题,我们更有可能提供帮助。 【参考方案1】:我不确定这是否是您要查找的内容,但是请按照一种方法获取重复值,并将其与 linq 分组;
您只需要创建一个控制台应用程序并将 newtonsoft.json 添加到其中,即可运行以下代码。
class Program
static string pathA = "<place-your-first-json-location-here>"; //ex: c:\test\test.json
static void Main(string[] args)
var json = JsonConvert.DeserializeObject<Test>(File.ReadAllText(pathA));
var filteredByName = json.Response.GroupBy(g => g.Name).OrderBy(o => o.Key).ToList();
foreach (var item in filteredByName)
Console.WriteLine($"Name: item.Key Qty response: item.Count()" );
public class Test
public string Status get; set;
public List<Response> Response get; set;
public class Response
public int Id get; set;
public string Name get; set;
public string SubName get; set;
public string Link get; set;
public int Active get; set;
public int Genre get; set;
public string Description get; set;
public string Languages get; set;
public int IsDeleted get; set;
public int GenreId get; set;
这是预期的输出,因此您可以继续并将其应用于您的逻辑;
Name: name 1 Qty response: 3
Name: name 2 Qty response: 1
Name: name 3 Qty response: 4
Name: name 4 Qty response: 1
【讨论】:
【参考方案2】:以下代码将生成您预期的输出: 此代码也位于 .net fiddle https://dotnetfiddle.net/d2uSNC
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
public class Program
public static void Main()
var jsonString = " \"status\": \"OK\", \"Response\": [ \"id\": 43, \"Name\": \"name 1\", \"SubName\": \"Sub 1\", \"Link\": \"Link 1\", \"Active\": 1, \"Genre\": 8, \"Description\": \"Description 1\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 8 , \"id\": 44, \"Name\": \"name 1\", \"SubName\": \"Sub 2\", \"Link\": \"Link 2\", \"Active\": 1, \"Genre\": 9, \"Description\": \"Description 2\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 9 , \"id\": 45, \"Name\": \"name 1\", \"SubName\": \"Sub 3\", \"Link\": \"Link 3\", \"Active\": 1, \"Genre\": 10, \"Description\": \"Description 3\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 10 , \"id\": 46, \"Name\": \"name 2\", \"SubName\": \"\", \"Link\": \"Link 4\", \"Active\": 1, \"Genre\": 11, \"Description\": \"Description 4\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 11 , \"id\": 47, \"Name\": \"name 3\", \"SubName\": \"Sub 1\", \"Link\": \"Link 5\", \"Active\": 1, \"Genre\": 12, \"Description\": \"Description 5\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 12 , \"id\": 48, \"Name\": \"name 3\", \"SubName\": \"Sub 2\", \"Link\": \"Link 6\", \"Active\": 1, \"Genre\": 13, \"Description\": \"Description 6\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 13 , \"id\": 49, \"Name\": \"name 3\", \"SubName\": \"Sub 3\", \"Link\": \"Link 7\", \"Active\": 1, \"Genre\": 14, \"Description\": \"Description 7\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 14 , \"id\": 50, \"Name\": \"name 3\", \"SubName\": \"Sub 4\", \"Link\": \"Link 8\", \"Active\": 1, \"Genre\": 15, \"Description\": \"Description 8\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 15 , \"id\": 51, \"Name\": \"name 4\", \"SubName\": \"\", \"Link\": \"Link 9\", \"Active\": 1, \"Genre\": 16, \"Description\": \"Description 9\", \"Languages\": \"\", \"IsDeleted\": 0, \"GenreId\": 16 ]";
var json = JsonConvert.DeserializeObject<Root>(jsonString);
var filteredByName = json.Response.GroupBy(g => g.Name).OrderBy(o => o.Key).ToList();
foreach (var item in filteredByName)
var fileObject = item.GroupBy(x=>x.SubName).Select(s=> new title =s.Key, items= s.ToList());
var fileJson = JsonConvert.SerializeObject(fileObject, Formatting.Indented);
Console.WriteLine("" + item.Key +".json");
Console.WriteLine(fileJson);
public class JsonResponse
public int id get; set;
public string Name get; set;
public string SubName get; set;
public string Link get; set;
public int Active get; set;
public int Genre get; set;
public string Description get; set;
public string Languages get; set;
public int IsDeleted get; set;
public int GenreId get; set;
public class Root
public string status get; set;
public List<JsonResponse> Response get; set;
【讨论】:
以上是关于如何使用 LINQ 中的键对值从单个字段中获取重复数据?的主要内容,如果未能解决你的问题,请参考以下文章