简体   繁体   English

分组并从列表中删除

[英]Grouping and remove from list

I have following type 我有以下类型

public class Parameter
{
   public string Name {get;set;}
   public string PathType {get;set;}
}

My parameter list like: 我的参数列表如:

List<Parameter> params = new List<Parameter>();
params.Add(new Parameter{ Name ="A", PathType ="path" });
params.Add(new Parameter{ Name ="B", PathType ="query" });
params.Add(new Parameter{ Name ="C", PathType ="path" });
params.Add(new Parameter{ Name ="C", PathType ="query" });
params.Add(new Parameter{ Name ="D", PathType ="path" });
params.Add(new Parameter{ Name ="D", PathType ="query" });
params.Add(new Parameter{ Name ="D", PathType ="body" });

I need to find parameters that have the same Name (like: C and D ) where PathType equals path and query . 我需要找到具有相同Name参数(如: CD ),其中PathType等于pathquery Sorry for my English, it is a little bit difficult to explain. 对不起我的英语,这有点难以解释。 I want to find 我想找到

 Name ="C", PathType ="path"
 Name ="C", PathType ="query"
 Name ="D", PathType ="path"
 Name ="D", PathType ="query"

Then remove all where PathType = "query" . 然后删除PathType = "query" Finally result should be 最后的结果应该是

Name ="A", PathType ="path" 
Name ="B", PathType ="query"
Name ="C", PathType ="path" 
Name ="D", PathType ="path" 
Name ="D", PathType ="body" 

My code 我的代码

var groupedParams = params.GroupBy(a => a.Name, StringComparer.InvariantCultureIgnoreCase).ToDictionary(t => t.Key);
foreach (var parameter in params) {
    if (groupedParams.ContainsKey(parameter.Name)) {
        var temp = groupedParams[parameter.Name];
        if (temp.Count(x => x.PathType.ToLower() == "path") == 1 && temp.Count(x => x.PathType.ToLower() == "query") == 1) {
            params.Remove(parameter);
        }
    }
}

I need to group by name but remove only when there are path and query for that name. 我需要按名称分组,但只有在存在该名称的路径和查询时才会删除。 Please suggest how can I do in in other way? 请建议我如何以其他方式做? Thanks 谢谢

I need to group by name but remove only when there are path and query for that name. 我需要按名称分组,但只有在存在该名称的路径和查询时才会删除。 Please suggest how can I do in in other way? 请建议我如何以其他方式做?

If I were to assume that phrase is equal to this: 如果我假设这句话与此相同:

How do I remove a parameter with PathType="query" from a collection, if and only if there's another parameter with the same Name AND PathType="path" ? 如果且仅当存在具有相同NamePathType="path"的另一个参数时,如何从集合中删除具有PathType="query"的参数?

Then you could do it with this statement: 然后你可以用这个声明来做:

params.RemoveAll(o => o.PathType == "query" && params.Any(p => p.Name == o.Name && p.PathType == "path"));

Update : 更新

For case insensitive comparison (on PathType ): 对于不区分大小写的比较(在PathType ):

params.RemoveAll(o => o.PathType.Equals("query", StringComparison.OrdinalIgnoreCase) && params.Any(p => p.Name == o.Name && p.PathType.Equals("path", StringComparison.OrdinalIgnoreCase)));

You could do that with this LINQ statement: 您可以使用此LINQ语句执行此操作:

var cleanedParams = params.Where(p => p.PathType != "query" ||
    !params.Any(p2 => p2.Name == p.Name && p2.PathType != "query")).ToList();

I have some difficulty to understand your problem as it is not well stated. 我很难理解你的问题,因为它没有得到很好的陈述。 So here is what i have understood. 所以这就是我所理解的。 It has 2 questions: Q1. 它有两个问题:Q1。 Return those entries which has repeated names (eg c and d) where pathType has value query or path like you mentioned in your expected result. 返回那些具有重复名称的条目(例如c和d),其中pathType具有值查询或路径,就像您在预期结果中提到的那样。

Name ="C", PathType ="path"
Name ="C", PathType ="query"
Name ="D", PathType ="path"
Name ="D", PathType ="query"

if yes you can get it like: 如果是,你可以得到它:

var filteredList = param.Where(x => x.PathType == "query" || x.PathType == "path").ToList();
//Below list will have result.
var duplicateNameList = filteredList.GroupBy(x => x.Name).Where(x => x.Count() > 1).ToList();

Q2. Q2。 Get the entries from the list where pathType =="query" or pathType=="path". 从列表中获取pathType ==“query”或pathType ==“path”的条目。

if yes here is the code: 如果是,这里是代码:

 var result = param.Where(x => x.PathType != "query" && x.PathType!="path").ToList();

Let me know if i have misunderstood your question. 如果我误解了你的问题,请告诉我。

Well, first of all, you can't use params as variable's name because it's C# instruction. 好吧,首先,你不能使用params作为变量的名称,因为它是C#指令。

Next. 下一个。 As I understand you want to have cycles. 据我所知你想要有周期。 Ok, as I noted above, you can't modify collection ( papram in your case) inside foreach loop body. 好吧,正如我上面提到的,你不能修改foreach循环体内的集合(在你的情况下为papram )。 You can use something like this: 你可以使用这样的东西:

var groupedParams = _params.GroupBy(a => a.Name, StringComparer.InvariantCultureIgnoreCase).ToDictionary(t => t.Key);
int i = 0;
while (i != _params.Count)
{
    if (groupedParams.ContainsKey(parameter.Name))
    {
        var temp = groupedParams[parameter.Name];
        if (temp.Count(x => x.PathType.ToLower() == "path") == 1 && temp.Count(x => x.PathType.ToLower() == "query") == 1)
        {
            params.RemoveAt(i);
            continue;
        }
    }
    i++;
}

I've just rename params to _params to make it more correct. 我只是将params重命名为_params以使其更正确。 If params is just Parametr[] replace _params.Count with _params.Length . 如果params只是Parametr[]_params.Count替换_params.Length

It seems I found the solution. 我似乎找到了解决方案。 My code : 我的代码:

var paramList = params.Where(x => x.PathType.Equals("path") || x.PathType.Equals("query"));
var groupedParams  = paramList.GroupBy(a => a.Name, StringComparer.InvariantCultureIgnoreCase).ToDictionary(t => t.Key);
foreach (var item in groupedParams) {
    var temp = item.Value.Count() > 1;
    if (temp) {
        var paramToDelete = operation.parameters.FirstOrDefault(x => x.Name.ToLower() == item.Key.ToLower() && x.PathType == "query");
        if (paramToDelete != null) {
            operation.parameters.Remove(paramToDelete);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM