我有以下代码不起作用:
var groupedZones = this._zoneDataManager.GetZonesGroupedByCountry();
IEnumerable<IGrouping<String, ZoneDTO>> zonesToReturn = Mapper.Map<IEnumerable<IGrouping<String, Zone>>, IEnumerable<IGrouping<String, ZoneDTO>>>(groupedZones);
Run Code Online (Sandbox Code Playgroud)
我一直得到以下异常:
值\"System.Collections.Generic.List
1[SCGRE.Business.Model.ZoneDTO]\" is not of type \"System.Linq.IGrouping2 [System.String,SCGRE.Business.Model.ZoneDTO] \"并且不能在此通用集合中使用.\ r \nParameter name:value
我不明白为什么它试图将一个映射List<T>成一个IGrouping<String, T>或者也许我没有正确理解异常...但我基本上有一个IEnumerable<IGrouping<String, Zone>>我希望将它映射到IEnumerable<IGrouping<String, ZoneDTO>>
请注意,我已经从创建的映射Zone到ZoneDTO如下:
Mapper.CreateMap<Zone, ZoneDTO>();
Run Code Online (Sandbox Code Playgroud)
那是因为两个类的属性几乎完全相同.
有任何想法吗?
我正在努力弄清楚如何检索实例的Value一部分IGrouping.
我有以下内容:
IList<IGrouping<string, PurchaseHistory> results = someList.GroupBy(x => x.UserName);
Run Code Online (Sandbox Code Playgroud)
现在,我希望遍历每个集合并检索该用户的购买历史记录(并检查购买历史记录中是否存在某些内容).
有人可以帮忙吗?
我想分组,然后在组内订购商品.
我怎么用lamda做的,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var data = new[]
{
new { Name="Tasty", Type="Strawberries", isAvail=true, Price=1.90m, Quantity=20 },
new { Name="Granny Smith", Type="Apple", isAvail=false, Price=0.80m, Quantity=7 },
new { Name="Gala", Type="Apple", isAvail=true, Price=0.75m, Quantity=10 }
};
var grouped = data.GroupBy(record => record.Type).OrderBy(x => x.Min(y => (Decimal)y.Price));
foreach (var group in grouped)
{
Console.WriteLine("Key {0}", group.Key);
foreach (var item in group)
{
Console.WriteLine("\t{0}", item.Name);
} …Run Code Online (Sandbox Code Playgroud) 所以我试图弄清楚传递给我的方法的任何随机IEnumerable是否已被分组.我的想法是,我将以不同的方式处理分组数据.我的问题是IGrouping<>通用接口没有实现非泛型接口,无论出于什么原因我都无法用is语句测试它.
要在此处测试这是我的示例代码:
var dict = new Dictionary<int, string>() {
{1, "Prime" },
{2, "Prime" },
{3, "Prime" },
{4, "Not" },
{5, "Prime" },
{6, "Not" },
{7, "Prime" },
{8, "Not" },
{9, "Not" },
{10, "Not" },
{11, "Prime" }
};
var grouped = from d in dict
group d by d.Value into gd
select gd;
Debug.WriteLine("IEnumerable<IGrouping<string, KeyValuePair<int, string>>>: {0}", grouped is IEnumerable<IGrouping<string, KeyValuePair<int, string>>>);
Debug.WriteLine("IEnumerable<IGrouping<object, KeyValuePair<int, string>>>: {0}", grouped is IEnumerable<IGrouping<object, KeyValuePair<int, string>>>); …Run Code Online (Sandbox Code Playgroud) 如何将SubGroup分组以创建大陆列表,其中每个大陆都有自己的县,每个国家/地区都有自己的城市,如此表
这是t-sql:
select Continent.ContinentName, Country.CountryName, City.CityName
from Continent
left join Country
on Continent.ContinentId = Country.ContinentId
left join City
on Country.CountryId = City.CountryId
Run Code Online (Sandbox Code Playgroud)
和t-sql的结果:
我尝试了这个,但它以错误的方式对数据进行分组,我需要像上表一样进行分组
var Result = MyRepository.GetList<GetAllCountriesAndCities>("EXEC sp_GetAllCountriesAndCities");
List<Continent> List = new List<Continent>();
var GroupedCountries = (from con in Result
group new
{
con.CityName,
}
by new
{
con.ContinentName,
con.CountryName
}
).ToList();
List<Continent> List = GroupedCountries.Select(c => new Continent()
{
ContinentName = c.Key.ContinentName,
Countries = c.Select(w => new Country()
{
CountryName = c.Key.CountryName,
Cities = c.Select(ww => new City() …Run Code Online (Sandbox Code Playgroud) 我目前有:
UIEnumerable <IGrouping<PortableKey, FileInfo>>
Run Code Online (Sandbox Code Playgroud)
从这段代码:
var queryDupFiles = from file in fileList
group file by new PortableKey { Name = file.Name, Length = file.Length }
into g
where g.Count() > 1
select g;
Run Code Online (Sandbox Code Playgroud)
PortableKey的代码是:
public class PortableKey
{
public string Name { get; set; }
public long Length { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上过滤和抓取所有重复的文件.我如何获得它以便以这种方式拥有所有FileInfo?
List<FileInfo> list
Run Code Online (Sandbox Code Playgroud) 我有一个IGrouping类型的对象,并希望在不改变对象类型的情况下对组内的元素进行排序.
换句话说,我有
var tmp = group.OrderBy(x => x);
Run Code Online (Sandbox Code Playgroud)
同group是型的IGrouping<int, someanonymousclass>,我想tmp为类型IGrouping<int, someanonymousclass>为好.
一种解决方法是将其更改为Dictionary或新的匿名类,但我希望tmp特别是类型IGrouping<int, someanonymousclass>.
换句话说:我想对我IGrouping<int, someanonymousclass>对象的元素进行排序.如果我使用OrderBy它改变的类型group来IOrderedEnumerable和我不能访问group.Key了.我怎么能维持这种类型group?
例:
var states = SimulationPanel.innerPanel.Children.OfType<StateBar>().Where(x => x.isSensorState())
.GroupBy(x => (SensorElement)x.BauplanElement,
x => new
{
Start = (decimal)(x.Margin.Left / SimulationPanel.Zoom),
Width = (decimal)(x.Width / SimulationPanel.Zoom),
State = x.State
});
var group = states.First();
var tmp = group.OrderBy(x => x.Start);
var key = tmp.Key; //This …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能List<IGrouping<string, IGrouping<string, T>>>从Linq表达式中获取.我想要做的是按流派和艺术家订购歌曲,但是我可以按照每种类型和每种类型进行迭代的格式,对每张专辑进行迭代.如果可能的话怎么办?或者有更好的方法来实现我想要达到的目标吗?
这是我的宋班
public class Song
{
[StringLength(100)]
public string Album { get; set; }
public int Id { get; set; }
[StringLength(100)]
public string Title { get; set; }
[StringLength(100)]
public string Genre { get; set; }
[StringLength(255)]
public string Comment { get; set; }
[StringLength(4)]
public string Year { get; set; }
[StringLength(100)]
public string Author { get; set; }
public int Length { get; set; }
[StringLength(255)]
public string Photo { get; set; } …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码来查找对象列表中的常见对象
https://dotnetfiddle.net/gCgNBf
..............................
var query = setOfPersons
.SelectMany(l => l.Select(l1 => l1))
.GroupBy(p => p.Id)
.Where(g => g.Count() == setOfPersons.Count);
Run Code Online (Sandbox Code Playgroud)
之后,我需要将"查询"转换为"人物"对象(列表)列表以实现其他目的.
我尝试使用"ToList()" ......但它说:
"无法将IGrouping转换为List".
有人可以帮我解决吗?
我有一个对象列表 list1
Name Value
N1 V100
N2 V101
N1 V102
N4 V103
N4 V104
Run Code Online (Sandbox Code Playgroud)
我想将其转换为分组列表
Name Values
N1 V100, V102
N2 V101
N4 V103, V104
Run Code Online (Sandbox Code Playgroud)
当我使用时,GroupBy我得到了整个东西,( list1.GroupBy(key => key.Name) )
Name Values
N1
N1 V100
N1 V102
N2
N2 V101
N4
N4 V103
N4 V104
Run Code Online (Sandbox Code Playgroud)
我只想将值作为List.
igrouping ×10
c# ×9
linq ×8
.net ×3
list ×2
automapper ×1
c#-4.0 ×1
collections ×1
fileinfo ×1
ienumerable ×1
lambda ×1
linq-group ×1
sql-order-by ×1