hi i am using silverlight/ria services with entity framework
i have my base orders partial class that i have extended with some porperties
public partial class ORDERS
{
public double SumOfDetails
{
get
{
if (ORDERS_DETAILS == null) return 0;
else return ORDERS_DETAILS.Sum(n => (double)n.OD_QTY * (double)n.OD_PRICE);
}
}
}
everything compile fine but i cannot bind/retrieve this new property
here the method that get the orders data
public IQueryable<ORDERSS> GetORDERS() { return this.ObjectContext.ORDERS.Include("ORDERSS_DETAILS").Include("CUSTOMERS").OrderByDescending(a => a.AC_DATE); }
btw how this standard sql query could be translated into linq :
Select c.CNT_NAME, A.id_order, a.ord_date, a.ord_customerID,
coalesce((select sum(d.od_price * d.ad_qty)
from orders_details d
where d.od_orderID = a.OrderID),0) as "total"
From orders a
join CUSTOMERS c on a.ord_id_customer = c.ID_Customer
group by c.CNT_NAME, A.orderID, a.ord_date, ord_CustomerID
order by ord_date desc
wich is simply retrieving all the orders and adding their sum from the order_details table
everything compile fine but i cannot bind/retrieve this new property
OrderByDesceding returns you an interface that has nothing to do with IQueryable<T>:
///<summary>Sorts the elements of a sequence in descending order according to a key.</summary>///<returns>An <see cref="T:System.Linq.IOrderedEnumerable`1" /> whose elements are sorted in descending order according to a key.</returns>///<param name="source">A sequence of values to order.</param>///<param name="keySelector">A function to extract a key from an element.</param>///<typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>///<typeparam name="TKey">The type of the key returned by <paramref name="keySelector" />.</typeparam>///<exception cref="T:System.ArgumentNullException">///<paramref name="source" /> or <paramref name="keySelector" /> is null.</exception>
[__DynamicallyInvokable]
publicstaticIOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(thisIEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
returnnew OrderedEnumerable<TSource, TKey>(source, keySelector, null, true);
}
But the interface can be converted to IEnumerable<T>, please change to this:
public IEnumerable<ORDERSS> GetORDERS() { return this.ObjectContext.ORDERS.Include("ORDERSS_DETAILS").Include("CUSTOMERS").OrderByDescending(a => a.AC_DATE); }
issam1975
Member
37 Points
129 Posts
binding computed properties
Nov 26, 2012 04:19 PM|LINK
hi i am using silverlight/ria services with entity framework
i have my base orders partial class that i have extended with some porperties
public partial class ORDERS { public double SumOfDetails { get { if (ORDERS_DETAILS == null) return 0; else return ORDERS_DETAILS.Sum(n => (double)n.OD_QTY * (double)n.OD_PRICE); } } }everything compile fine but i cannot bind/retrieve this new property
here the method that get the orders data
public IQueryable<ORDERSS> GetORDERS()
{
return this.ObjectContext.ORDERS.Include("ORDERSS_DETAILS").Include("CUSTOMERS").OrderByDescending(a => a.AC_DATE);
}
btw how this standard sql query could be translated into linq :
wich is simply retrieving all the orders and adding their sum from the order_details table
thanks and good day
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: binding computed properties
Nov 27, 2012 03:47 AM|LINK
Hello,
First, you don't need to use "else" here to simply your codes like this:
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: binding computed properties
Nov 27, 2012 03:50 AM|LINK
OrderByDesceding returns you an interface that has nothing to do with IQueryable<T>:
But the interface can be converted to IEnumerable<T>, please change to this:
public IEnumerable<ORDERSS> GetORDERS() { return this.ObjectContext.ORDERS.Include("ORDERSS_DETAILS").Include("CUSTOMERS").OrderByDescending(a => a.AC_DATE); }
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: binding computed properties
Nov 27, 2012 03:52 AM|LINK
1)You can try to use the Auto-LINQ convertor:http://www.sqltolinq.com/
2)if that fails, this means your current SQL cannot be converted a proper LINQ. So please try to use directly ADO.NET (With SqlCommand) instead.