Linq - 当子记录由不同func生成的查询时,sum子字段值

时间:2009-01-26 11:33:55

标签: .net linq iqueryable

我有订单 - 商品表。 所以我想在一些网格上显示所有订单信息,包括像total_items

这样的col

有一种预防性的工作方式。像这样:

TotalQuantity = 
  (from i in _db.ProposaItems 
   where i.ProposaID == p.ProposaID select i)
  .Sum(q => q.Quantity)

但这不是我想要的方式。 我想使用2个功能:

功能1:

//BizNet.SqlRepository.Data.ProposalItem        
public IQueryable<ProposaItem> GetItems(Guid ProposaID)
{
  return from i in _db.ProposaItems
         where i.ProposaID == ProposaID
         select i;
}

功能2。

public void GetProposas()
{
  var x = from p in _db.Proposas
          let t= GetItems(p.ProposaID)
                 .Sum(q => q.Quantity)
          select new 
          {
            ID = p.ProposaID,
            TotalQuantity = t
          };
}

对我来说,它看起来很简单。 但是在行

x.Count();

结果是异常。

"Member access 'Int16 Quantity' of 'BizNet.SqlRepository.Data.ProposaItem' not legal on type 'System.Linq.IQueryable`1[BizNet.SqlRepository.Data.ProposaItem]."

"   at System.Data.Linq.SqlClient.SqlMember.set_Expression(SqlExpression value)\r\n   at System.Data.Linq.SqlClient.SqlFactory.Member(SqlExpression expr, MemberInfo member)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.AccessMember(SqlMember m, SqlExpression expo)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitMember(SqlMember m)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitUnaryOperator(SqlUnary uo)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSimpleExpression(SqlSimpleExpression simple)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitUnaryOperator(SqlUnary uo)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.VisitSequence(SqlSelect sel)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.VisitScalarSubSelect(SqlSubSelect ss)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.VisitSubSelect(SqlSubSelect ss)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSubSelect(SqlSubSelect ss)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitNew(SqlNew sox)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitAlias(SqlAlias a)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitAlias(SqlAlias a)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitIncludeScope(SqlIncludeScope scope)\r\n   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlBinder.Bind(SqlNode node)\r\n   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations)\r\n   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)\r\n   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)\r\n   at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)\r\n   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)"

对此有一些解释?

感谢。

2 个答案:

答案 0 :(得分:1)

我认为你不能在LINQ to SQL查询中调用未映射的函数(例如GetItems)。

答案 1 :(得分:0)

在linqtoSql查询中,GetItems func应该没问题,因为它返回一个IQueryable本身。如果这是一个问题,你很可能得到一个“不支持”的例外而不是你得到的例外。

但看起来你真正想要的是所有产品ID的列表及其相关数量的总结。如果是这样,那么我认为你应该考虑使用linq组。据我所知,你可以在一个查询中写下你想要做的事情:

var x = from p in _db.ProposaItems
         group p by p.ProposaID into g
         select new
         {
           ID = g.Key,
           TotalQuantity = g.Sum(c => c.Quantity)
         };