我无法使用LINQ查询解决此问题。
因此,我们拥有Book的表结构如下:
LibraryId || LibraryName || ShelveId || ShelveName || Cost || Name || ForeName || Stuff我想先按图书馆分组,然后按书架分组。图书馆是书架的列表,书架是书籍的列表。性能很重要。这是真实数据的片段。
var table = new[] {
new Book (1, "Green", 42, "A", 10, "Gra", "Bar", "etc."),
new Book (1, "Green", 43, "B", 21, "Grb", "Bar", "etc."),
new Book (2, "Blue", 652, "C", 10, "Blc", "Bar", "etc."),
new Book (2, "Blue", 652, "C", 01, "Bl2", "Bar", "etc."),
new Book (2, "Blue", 123, "D", 12, "Bld", "Bar", "etc."),
new Book (8, "White", 94, "E", 14, "Foo", "Bar", "etc."),
new Book (9, "Grey", 142, "F", 11, "Foo", "Bar", "etc."),
new Book (9, "Grey", 142, "F", 12, "Bar", "Bar", "etc.")
};类:
class Book
{
public int LibraryId {get;set;}
public string LibraryName {get;set;}
public int ShelveId {get;set;}
public string ShelveName {get;set;}
public int Cost {get;set;}
public string Name {get;set;}
public string ForeName {get;set;}
public string Stuff {get;set;}
public Book(int libraryId, string libraryName, int shelveId, string shelveName
, int cost, string name, string foreName, string stuff)
{
LibraryId = libraryId;
LibraryName = libraryName;
ShelveId = shelveId;
ShelveName = shelveName;
Cost = cost;
Name = name;
ForeName = foreName;
Stuff = stuff;
}
}
class Library
{
public int Id {get;set;}
public string Name {get;set;}
public int Cost {get;set;}
public List<Shelve> Shelves {get;set;}
public Library (Shelve shelve)
{
Id = shelve.Books[0].LibraryId;
Name = shelve.Books[0].LibraryName;
Cost = shelve.Cost;
Shelves = new List<Shelve> {shelve};
}
}
class Shelve
{
public int Id {get;set;}
public string Name {get;set;}
public int Cost {get;set;}
public List<Book> Books {get;set;}
public Shelve (Book book)
{
Id = book.ShelveId;
Name = book.ShelveName;
Cost = book.Cost;
Books = new List<Book> {book};
}
}我用一个很好的老foreach实现了我的行为
var libraries = new List<Library> { new Library (new Shelve(table[0])) };
foreach (var item in table.Skip(1))
{
if (item.LibraryId != libraries.Last().Id)
{
libraries.Add(new Library(new Shelve(item)));
continue;
}
if (item.ShelveId != libraries.Last().Shelves.Last().Id)
{
libraries.Last().Cost += item.Cost;
libraries.Last().Shelves.Add(new Shelve(item));
continue;
}
libraries.Last().Cost += item.Cost;
libraries.Last().Shelves.Last().Cost += item.Cost;
libraries.Last().Shelves.Last().Books.Add(item);
}
var total = libraries.Sum(x => x.Cost);据我所知,这是一个O(n)解。
现在,我想对它进行重构。我想我们可以对GroupBy做点什么。我试过这个:
var grouped = table
.GroupBy(l => new { l.ShelveId, l.LibraryId})
.GroupBy(l => l.Key.LibraryId);
foreach(var country in grouped)
{
foreach(var state in country)
{
foreach(var personInState in state)
{
Console.WriteLine(personInState.Name);
}
}
}但我不知道如何将成本相加,并将LibraryName和ShelveName添加到每个Library和Shelve中。
Try it Online! (带基本测试)
发布于 2018-07-20 14:38:40
也许您可以通过使用计算属性来简化它,如下所示
class Shelve
{
public int Id {get;set;}
public string Name {get;set;}
public List<Book> Books {get;set;} = List<Book>();
public int Cost
{
get{ return this.Books.Sum(x => x.Cost); }
}
}您也可以对Library类执行相同的操作...
class Library
{
public int Id {get;set;}
public string Name {get;set;}
public List<Shelve> Shelves {get;set;} = new List<Shelve>();
public int Cost
{
get{ return this.Shelves.Sum(x => x.Cost); }
}
}我认为这将使您的业务逻辑代码更加清晰,并且您不需要为复杂的group by表达式而头疼
https://stackoverflow.com/questions/51436246
复制相似问题