列出Occurence明智阅读c#

时间:2016-11-05 15:50:49

标签: c# algorithm linq c#-4.0

我有类似这样的课程

  public  class RoomDetails
  {
     public int RoomIndexID {get;set;} --(This is No of Rooms)
     public int roomcategoryID {get;set;} -- (This is No of Category that room have)
     public decimal roomPrice{get;set;}
     public string roomCategory{get;set;}
  }

所以我有这样的RoomDetails列表(数据)

  • 1,1,200,单人床
  • 2,1,250,双人床
  • 2,2,400,双人床,额外景观
  • 2,4,530,Tripple Bed
  • 3,1,530,Tripple Bed
  • 3,2,600,三人大床

所以我想按照程序阅读这份清单。

  • 1,1,200,单人床
  • 2,1,250,双人床
  • 3,1,530,Tripple Bed
  • 1,1,200,单人床
  • 2,1,250,双人床
  • 3,2,600,三人大床
  • 1,1,200,单人床
  • 2,2,400,双人床,额外景观
  • 3,1,530,Tripple Bed
  • 1,1,200,单人床
  • 2,2,400,双人床,额外景观
  • 3,2,600,三人大床
  • 1,1,200,单人床
  • 2,4,530,Tripple Bed
  • 3,1,530,Tripple Bed
  • 1,1,200,单人床
  • 2,4,530,Tripple Bed
  • 3,2,600,三人大床

请问您是否有任何问题重新解决我的问题。

这是示例

我的数据列表是这样的

  • A室 - A1
  • B室 - B1,B2,B3
  • C室 - C1,C2

(实际上A,B,C表示RoomIndexID' s和A1,B1,B2,B3,C1,C2是roomcategoryID' s) 所以我想这样读。

  • A1,B1,C1
  • A1,B1,C2
  • A1,B2,C1
  • A1,B2,C2
  • A1,B3,C1
  • A1,B3,C2

2 个答案:

答案 0 :(得分:1)

如果你想要" LINQy"解决方案,它会有点复杂。请注意,它是LINQ对象,因此如果您有.ToList()(例如来自EntityFramework),则需要先调用IQueryable

var total = list.GroupBy(x=>x.RoomIndexID)
    .Select(x=>x.Count())
    .Aggregate(1, (s,d)=>s*d);

var counts = list.GroupBy(x => x.RoomIndexID)
    .ToDictionary(x => x.Key, x => x.Count());

var nums = list.GroupBy(x => x.RoomIndexID)
    .Select(x => x.Select((p, i) => new {i, p}))
    .SelectMany(x => x)
    .ToList();

var result = Enumerable.Range(0, total)
    .Select(x =>
        nums.Where(y => {               
            var c = counts[y.p.RoomIndexID];
            var p = counts
                      .Where(z => z.Key > y.p.RoomIndexID)
                      .Aggregate(1, (s,d)=>s*d.Value);

            return y.i == x/p%c;
        })
        .Select(y => y.p)
    );

在这里演示:https://dotnetfiddle.net/udf6VA

答案 1 :(得分:0)

更简单,类似这样的事情:

var groups = list // your input
    .GroupBy(x => x.RoomIndexID)
    .Select(x => x.ToList())
    .ToList();

var result = groups.Skip(1).Aggregate(
    groups.Take(1),
    (x, y) => x.SelectMany(z => y, (a, b) => a.Concat(new[] { b }).ToList()));

基本上,我们的想法是首先GroupBy房间索引,以便获得相同类型的内部序列,然后在这些序列之间执行交叉连接(SelectMany对它来说没问题),最后{{ 1}}联合结果。这三个步骤应该没问题。所涉及的步骤可以通过以下命令式样式示例更清晰:

Aggregate

如果我们对var groups = list .GroupBy(x => x.RoomIndexID) .Select(x => x.ToList()); IEnumerable<IEnumerable<RoomDetails>> result = null; foreach (var item in groups) { if (result == null) result = new[] { item }; else result = result.SelectMany(x => item, (x, y) => x.Concat(new[] { y })); } 进行了适当的重载,那么就可以在一步中完成这项工作,这会使序列中的Aggregate第一项作为种子。

Func

由于框架或语言中的一些缺失部分,整个事情看起来很复杂。理想情况下(在我假设的许多函数式语言中)你应该能够做到:

// Say you have:
public static TAccumulate Aggregate<TSource, TAccumulate>(
    this IEnumerable<TSource> source,
    Func<TSource, TAccumulate> seeder,
    Func<TAccumulate, TSource, TAccumulate> func)
{
    if (source == null)
        throw new ArgumentNullException(nameof(source));

    if (func == null)
        throw new ArgumentNullException(nameof(func));

    using (IEnumerator<TSource> iterator = source.GetEnumerator())
    {
        if (!iterator.MoveNext())
            throw new InvalidOperationException("Empty sequence.");

        TAccumulate result = seeder(iterator.Current);
        while (iterator.MoveNext())
        {
            result = func(result, iterator.Current);
        }

        return result;
    }
}

// Then you could do: 
var result = list
    .GroupBy(x => x.RoomIndexID)
    .Select(x => x.ToList())
    .Aggregate(
        x => Enumerable.Repeat(x, 1),
        (x, y) => x.SelectMany(z => y, (a, b) => a.Concat(new[] { b }).ToList()));