0

我希望能够对子 linq 查询进行分组,但 LinqPad 在这一行给出错误:

ratetocharge =rtc.rate.Sum

错误是:无法将方法组分配给匿名类型属性。

任何人都可以建议我应该在该行中输入什么,以便给我费率表中的费率总和吗?

谢谢,

标记

var mtgRooms = RoomUnit
        .Where(r => r.building_id==1)
        .GroupBy(p => p.Type)
        .Select(g => new
            {
            TypeName = g.Key.type_name,
            TypeID = g.Key.type_id,
            TypeCount = g.Count(),
            rates = rates

        .Select (  rtc => 
                 new  
                 {
                    occ = rtc.occ, 
                    ratetocharge =rtc.rate.Sum  // this is the line which errors
                 }
                   )
                .GroupBy(pe => pe.occ)
             }
            );

            mtgRooms.Dump();

编辑:添加更多细节

表:

桌房单位

type_id (key - int)
type_name (string - eg. large room, small room, medium room)
building_id (int - denotes a number of buildings this room could be within)

费率

rate_id
type_id (foreign key to RoomUnit)
occ (type of rate - ie. Desk, FullRoom)

这个想法是我想提供一个看起来像这样的模型:

Type of Room
Number of Types of Room available
Type of Occ/Desks available
Sum of Rate (rate to charge) for each type of desk

目前显示的是:

LargeRoom, Type 3049, Type Count 18 (or whatever is available)
    occ FullRoom, ratetocharge 250, numOfOcc 1
    occ FullRoom, ratetocharge 250, numOfOcc 1
    occ FullRoom, ratetocharge 250, numOfOcc 1
    occ Single Desk, ratetocharge 45, numOfOcc 1
    occ Single Desk, ratetocharge 45, numOfOcc 1

SmallRoom, Type 3093, Type Count 4 (or whatever is available)
    occ FullRoom, ratetocharge 150, numOfOcc 1
    occ FullRoom, ratetocharge 150, numOfOcc 1
    occ Single Desk, ratetocharge 45, numOfOcc 1
    occ Single Desk, ratetocharge 45, numOfOcc 1
    occ Single Desk, ratetocharge 45, numOfOcc 1

而我希望模型在子表中按 Occ 对 rateToCharge 求和,例如:

LargeRoom, Type 3049, Type Count 18 (or whatever is available)
    occ FullRoom, ratetocharge 750, numOfOcc 3
    occ Single Desk, ratetocharge 90, numOfOcc 2

SmallRoom, Type 3093, Type Count 4 (or whatever is available)
    occ FullRoom, ratetocharge 300, numOfOcc 2
    occ Single Desk, ratetocharge 135, numOfOcc 3
4

1 回答 1

2

更改ratetocharge =rtc.rate.Sumratetocharge =rtc.rate.Sum(). 使用Sum编译器认为您正在尝试将 ratetocharge 变量分配给方法组;方法的方法集(包括重载)Sum。相反,您需要进行Sum()实际调用该方法的调用。

---编辑---查看您的更多信息,我将以此作为实施方式

var mtgRooms = RoomUnit
.Where(r => r.building_id==1)
.GroupBy(p => p.Type)
.Select(g => new
    {
    TypeName = g.Key.type_name,
    TypeID = g.Key.type_id,
    TypeCount = g.Count(),
    rates = Rates.Where(rt => rt.type_id == g.type_id).GroupBy(rt => rt.occ)
            .Select(proj => new  {
                occ = proj.Key,
                ratetocharge = proj.Sum(s => s.rate),
                numOfOcc = proj.count())                    
            })              
     }
);
于 2012-08-09T11:34:30.327 回答