.net Linq-当子记录由Different func查询时求和子字段值
我有订单项目表。 所以我想在一些网格上显示所有订单信息,包括一个col,比如total_items 有一种积极的工作方式。像这样:.net Linq-当子记录由Different func查询时求和子字段值,.net,linq,iqueryable,.net,Linq,Iqueryable,我有订单项目表。 所以我想在一些网格上显示所有订单信息,包括一个col,比如total_items 有一种积极的工作方式。像这样: TotalQuantity = (from i in _db.ProposaItems where i.ProposaID == p.ProposaID select i) .Sum(q => q.Quantity) var x = from p in _db.ProposaItems group p by p.Propo
TotalQuantity =
(from i in _db.ProposaItems
where i.ProposaID == p.ProposaID select i)
.Sum(q => q.Quantity)
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)
};
但这不是我想要的方式。
我想使用2个函数:
职能1:
//BizNet.SqlRepository.Data.ProposalItem
public IQueryable<ProposaItem> GetItems(Guid ProposaID)
{
return from i in _db.ProposaItems
where i.ProposaID == ProposaID
select i;
}
对我来说,它看起来很简单。
但是排队
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)"
有什么解释吗
谢谢。我认为您不能在LINQ-to-SQL查询中调用未映射的函数(例如
GetItems
)。在linqtoSql查询中,GetItems函数应该可以,因为它本身返回一个IQueryable。如果这是一个问题,您很可能会得到一个“不受支持”的异常,而不是您正在得到的异常
但看起来您实际需要的是所有产品ID的列表及其相关数量的总和。如果是这样,那么我认为您应该考虑使用linq组。据我所知,您可以在一个查询中编写您正在尝试执行的操作,如下所示:
TotalQuantity =
(from i in _db.ProposaItems
where i.ProposaID == p.ProposaID select i)
.Sum(q => q.Quantity)
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)
};
您必须准确地告诉我们您得到了什么异常以及代码中的哪一行。