C# MVC 获取 .不同() 数据
本文关键字:数据 不同 MVC 获取 | 更新日期: 2023-09-27 18:35:43
我在SQL Server中有一些数据:
[
{
"code": 1,
"name": "qweee",
"IDNO": "ff2222"
},
{
"code": 2,
"name": "test2",
"IDNO": "ff2222211"
},
{
"code": 3,
"name": "fsdfasdf",
"IDNO": "ddaaa"
},
{
"code": 4,
"name": "qweee",
"IDNO": "ff2222"
},
{
"code": 5,
"name": "123",
"IDNO": "U120000005"
}
]
我需要在 C# MVC 中使用以下 SQL
SELECT DISTINCT
A1.IDNO , A1.name ,
(SELECT COUNT(cnt_tab.IDNO)
FROM Postpartum_AccountsManagement AS cnt_tab
WHERE cnt_tab.IDNO = A1.IDNO) AS data_cnt
FROM
Postpartum_AccountsManagement AS A1
然后我会得到
[
{
"name": "test2",
"IDNO": "ff2222211",
"data_cnt": 1
},
{
"name": "qweee",
"IDNO": "ff2222",
"data_cnt": 2
},
{
"name": "fsdfasdf",
"IDNO": "ddaaa",
"data_cnt": 1
},
{
"name": "123",
"IDNO": "U120000005",
"data_cnt": 1
}
]
我需要在 C# MVC 中获取相同的数据。
请检查
以下代码
使用 NewtonSoft 将 json 字符串转换为序列化对象并使用 Linq 查询。
IList<AccountsManagement> yourData = JsonConvert.DeserializeObject<List<AccountsManagement>>(yourJsonString);
IList<YourResult> finalResult = yourData.GroupBy(m => new {m.Name, m.IDNO).Select(group => new YourResult(){
Name = group.FirstOrDefault().Name,
IDNO = group.FirstOrDefault().IDNO,
Data_cnt = group.Count()
}).ToList();
您的课程将是
public class AccountsManagement
{
public int Code {get;set;}
public string Name {get;set;}
public string IDNO {get;set;}
}
public class YourResult
{
public string Name {get;set;}
public string IDNO {get;set;}
public int Data_cnt {get;set;}
}