Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免在C中重复按Linq分组#_C#_Asp.net Mvc_Linq_Optimization - Fatal编程技术网

C# 避免在C中重复按Linq分组#

C# 避免在C中重复按Linq分组#,c#,asp.net-mvc,linq,optimization,C#,Asp.net Mvc,Linq,Optimization,我需要优化我的代码。我有一些重复代码。但我想优化它。谁能帮我优化我的代码。我怎样才能把这个常用函数变大 foreach (var item in hotellocation.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count())) { if (item.Key != "") { lstHote

我需要优化我的代码。我有一些重复代码。但我想优化它。谁能帮我优化我的代码。我怎样才能把这个常用函数变大

 foreach (var item in hotellocation.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()))
            {
                if (item.Key != "")
                {
                    lstHotelLocation.Add(new HotelLocation()
                        {
                            Name = item.Key,
                            count = item.Value
                        });
                }
            }

            //need to Apply to linq

            foreach (var item in hoteltype.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count()))
            {               
                if (item.Key != "")
                {
                    lstHotelType.Add(new HotelTypeFilter()
                    {
                        Name = item.Key,
                        count = item.Value
                    });
                }
            }

首先要做的是去掉那些
foreach
循环,因为它们与LINQ不一致,并抛弃字典,因为它毫无意义:

var lstHotelLocation = hotellocation.GroupBy(x => x)
                                    .Where(g => g.Key != "")
                                    .Select(g => new HotelLocation {
                                        Name = kv.Key,
                                        count = g.Count()
                                    })
                                    .ToList();

 var lstHotelType = hoteltype.GroupBy(x => x)
                             .Where(g => g.Key != "")
                             .Select(g => new HotelTypeFilter {
                                 Name = g.Key,
                                 count = g.Count()
                             })
                             .ToList();
如果要进一步删除副本,可以执行以下操作:

static List<T> AssembleCounts<T>(IEnumerable<string> values, 
                                 Func<string, int, T> makeObject)
{
    return values.Where(x => !string.IsNullOrEmpty(x))
                 .GroupBy(x => x)
                 .Select(g => makeObject(g.Key, g.Count()))
                 .ToList();
}

var lstHotelLocation = AssembleCounts(hotellocation,
                                      (k, c) => new HotelLocation {
                                          Name = k, count = c
                                      });

var lstHotelType = AssembleCounts(hoteltype,
                                  (k, c) => new HotelTypeFilter {
                                       Name = k, count = c
                                  });
静态列表集合计数(IEnumerable值,
Func(对象)
{
返回值。其中(x=>!string.IsNullOrEmpty(x))
.GroupBy(x=>x)
.Select(g=>makeObject(g.Key,g.Count())
.ToList();
}
var lstHotelLocation=AssembleCounts(hotellocation,
(k,c)=>新酒店位置{
Name=k,count=c
});
var lstHotelType=AssembleCounts(hoteltype,
(k,c)=>新的HotelTypeFilter{
Name=k,count=c
});

首先要做的是去掉那些
foreach
循环,因为它们与LINQ不一致,并且放弃字典,因为它是毫无意义的:

var lstHotelLocation = hotellocation.GroupBy(x => x)
                                    .Where(g => g.Key != "")
                                    .Select(g => new HotelLocation {
                                        Name = kv.Key,
                                        count = g.Count()
                                    })
                                    .ToList();

 var lstHotelType = hoteltype.GroupBy(x => x)
                             .Where(g => g.Key != "")
                             .Select(g => new HotelTypeFilter {
                                 Name = g.Key,
                                 count = g.Count()
                             })
                             .ToList();
如果要进一步删除副本,可以执行以下操作:

static List<T> AssembleCounts<T>(IEnumerable<string> values, 
                                 Func<string, int, T> makeObject)
{
    return values.Where(x => !string.IsNullOrEmpty(x))
                 .GroupBy(x => x)
                 .Select(g => makeObject(g.Key, g.Count()))
                 .ToList();
}

var lstHotelLocation = AssembleCounts(hotellocation,
                                      (k, c) => new HotelLocation {
                                          Name = k, count = c
                                      });

var lstHotelType = AssembleCounts(hoteltype,
                                  (k, c) => new HotelTypeFilter {
                                       Name = k, count = c
                                  });
静态列表集合计数(IEnumerable值,
Func(对象)
{
返回值。其中(x=>!string.IsNullOrEmpty(x))
.GroupBy(x=>x)
.Select(g=>makeObject(g.Key,g.Count())
.ToList();
}
var lstHotelLocation=AssembleCounts(hotellocation,
(k,c)=>新酒店位置{
Name=k,count=c
});
var lstHotelType=AssembleCounts(hoteltype,
(k,c)=>新的HotelTypeFilter{
Name=k,count=c
});

我要摆脱的第一件事是
ToDictionary
-它毫无意义,因为你从来没有把它当作字典使用。我要摆脱的第一件事是
ToDictionary
-它毫无意义,因为你从来没有把它当作字典使用。
count=g.Value
需要更改为
count=g.count()
(仅第二个示例)如果您已删除字典,则如果您已删除字典,则需要将
count=g.Value
更改为
count=g.count()
(仅第二个示例)