views:

1246

answers:

2

Hi, 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.

+1  A: 

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

Justice
Do you have a better way?
ari
A: 

The GetItems func should be ok in a linqtoSql query because it returns an IQueryable itself. If it was a problem you would most likely get a "not supported" exception instead of the one you are getting.

But it looks like what you actually want is a list of all the product IDs with their associated quantities summed up. If so, then I think you should look at using linq group instead. As far as I can tell, you could write what you are trying to do in a single query like this:

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)
         };
Jero