c# - 过滤字符串列表并仅选择多次出现的字符串的一次出现
问题描述
var paymentTypes = _context
.BursaryTransactions
.Select(c => c.PaymentType)
.ToList();
string[] obj = paymentTypes
.ToArray();
var a = obj[1];
第一行从 Table 中检索以字符串形式存在的支付类型列表,BursaryTransactions
第二行将列表转换为数组。
但是,第一行的列表包含类似的字符串,例如
- 发布 Utme
- 学费
- 学费
- 发布 Utme
- 表格
- 表格
我想过滤这些列表并只检索一次出现多次的项目。然后将新列表转换为数组。
解决方案
您可以尝试GroupBy
选择包含多个1
项目的组:
var result = _context
.BursaryTransactions
.GroupBy(c => c.PaymentType) // Group By PaymentType
.Where(group => group.Count() > 1) // groups with more than 1 item
.Select(group => group.First()) // we want just the 1st item of such group
.ToList(); // materialized as a List<T>
编辑:要删除重复项,我们可以First
从每个项中获取项目group
:
var result = _context
.BursaryTransactions
.GroupBy(c => c.PaymentType)
.Select(group => group.First()) // First().PaymentType if you want PaymentType only
.ToList();
推荐阅读
- angular - 设置 iframe 源地址
- reactjs - Next js import scss error(ModuleParseError: Module parse failed: Unexpected character '�' (1:0))
- angular - 在拖放中获取div坐标
- ios - SwiftUI 列表中某些行的布局问题
- sql - 合并语法不正确
- oracle - ORA-40474: JSON 数据中的 UTF-8 字节序列无效
- angular - 如何将样式信息传递给网格?
- javascript - 如何将 Firestore 与 React-native 一起使用?
- sql - PL/SQL:在一个表上创建触发器以更新包含在单独表中的项目数量
- html - 我不明白为什么我的背景图像的 Css 代码不起作用