Linq中子查询中的Sum字段给出了Can not Assign Method错误

时间:2012-08-09 11:25:32

标签: linq entity-framework linq-to-sql

我希望能够对sub-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();

编辑:添加更多详细信息

表:

Table RoomUnit

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

1 个答案:

答案 0 :(得分:2)

ratetocharge =rtc.rate.Sum更改为ratetocharge =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())                    
            })              
     }
);