在列表 C# 中搜索特定的重复键
Search Specific Duplicated keys in List C#
我有一个名为 "listA" 的列表,其中包含 1 个名为 "fieldA" 的字段,数据为
{1,0,0,0,1}
我只想检测“1”是否重复,如果“0”重复则不算重复
我试试
var dupl = listA
.GroupBy(i => i.fieldA=="1")
.Where(g => g.Count() > 1)
.Select(g => g.Key).ToList();
if (dupl.Count()>0){"you have duplicated 1"}
但“0”仍被检测为重复,我的 linq 有什么问题?
如果你只想知道是否有重复的 1 那么只需使用 Count
:
bool isDuplicate = listA.Count(x => x.fieldA == "1") > 1;
你可以试试这个:
bool oneIsDuplicate = listA.Where(x=>x.fieldA=="1").Count() > 1;
这篇文章可能会有帮助。
这里我们避免了在到达集合结束之前满足条件时的额外迭代。(我们不迭代整个集合以防在集合结束之前满足条件因此它有点多
明智地调整性能)。
public static class Extentions
{
/// <summary>
/// Used to find if an local variable aggreation based condition is met in a collection of items.
/// </summary>
/// <typeparam name="TItem">Collection items' type.</typeparam>
/// <typeparam name="TLocal">Local variable's type.</typeparam>
/// <param name="source">Inspected collection of items.</param>
/// <param name="initializeLocalVar">Returns local variale initial value.</param>
/// <param name="changeSeed">Returns desired local variable after each iteration.</param>
/// <param name="stopCondition">Prediate to stop the method execution if collection hasn't reached last item.</param>
/// <returns>Was stop condition reached before last item in collection was reached.</returns>
/// <example>
/// var numbers = new []{1,2,3};
/// bool isConditionMet = numbers.CheckForLocalVarAggreatedCondition(
/// () => 0, // init
/// (a, i) => i == 1 ? ++a : a, // change local var if condition is met
/// (a) => a > 1);
/// </example>
public static bool CheckForLocalVarAggreatedCondition<TItem, TLocal>(
this IEnumerable<TItem> source,
Func<TLocal> initializeLocalVar,
Func<TLocal, TItem, TLocal> changeSeed,
Func<TLocal, bool> stopCondition)
{
TLocal local = default(TLocal);
foreach (TItem item in source)
{
local = changeSeed(local, item);
if (stopCondition(local))
{
return true;
}
}
return false;
}
}
我有一个名为 "listA" 的列表,其中包含 1 个名为 "fieldA" 的字段,数据为
{1,0,0,0,1}
我只想检测“1”是否重复,如果“0”重复则不算重复
我试试
var dupl = listA
.GroupBy(i => i.fieldA=="1")
.Where(g => g.Count() > 1)
.Select(g => g.Key).ToList();
if (dupl.Count()>0){"you have duplicated 1"}
但“0”仍被检测为重复,我的 linq 有什么问题?
如果你只想知道是否有重复的 1 那么只需使用 Count
:
bool isDuplicate = listA.Count(x => x.fieldA == "1") > 1;
你可以试试这个:
bool oneIsDuplicate = listA.Where(x=>x.fieldA=="1").Count() > 1;
这篇文章可能会有帮助。 这里我们避免了在到达集合结束之前满足条件时的额外迭代。(我们不迭代整个集合以防在集合结束之前满足条件因此它有点多 明智地调整性能)。
public static class Extentions
{
/// <summary>
/// Used to find if an local variable aggreation based condition is met in a collection of items.
/// </summary>
/// <typeparam name="TItem">Collection items' type.</typeparam>
/// <typeparam name="TLocal">Local variable's type.</typeparam>
/// <param name="source">Inspected collection of items.</param>
/// <param name="initializeLocalVar">Returns local variale initial value.</param>
/// <param name="changeSeed">Returns desired local variable after each iteration.</param>
/// <param name="stopCondition">Prediate to stop the method execution if collection hasn't reached last item.</param>
/// <returns>Was stop condition reached before last item in collection was reached.</returns>
/// <example>
/// var numbers = new []{1,2,3};
/// bool isConditionMet = numbers.CheckForLocalVarAggreatedCondition(
/// () => 0, // init
/// (a, i) => i == 1 ? ++a : a, // change local var if condition is met
/// (a) => a > 1);
/// </example>
public static bool CheckForLocalVarAggreatedCondition<TItem, TLocal>(
this IEnumerable<TItem> source,
Func<TLocal> initializeLocalVar,
Func<TLocal, TItem, TLocal> changeSeed,
Func<TLocal, bool> stopCondition)
{
TLocal local = default(TLocal);
foreach (TItem item in source)
{
local = changeSeed(local, item);
if (stopCondition(local))
{
return true;
}
}
return false;
}
}