R – Linq – sum child field value when child records query ganareted by diffrent func

.netiqueryablelinq

I have order – items tables.
so I want to display on some grid all the orders info including a col like total_items

There is a premetive working way. like this:

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

But this is not the way I want.
I want to use 2 funcs:

Function 1:

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

Function 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
          };
}

For me its look very simple.
But in the line

x.Count();

The result is Exception.

"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)"

There is some explanation for this?

thanks.

Best Solution

I don't think you can call unmapped functions (such as GetItems) in a LINQ to SQL query.

Related Question