Returning type from Linq query

3 posts, 0 answers
  1. Chris Williams
    Chris Williams avatar
    63 posts
    Member since:
    Oct 2004

    Posted 16 Dec 2009 Link to this post

    Hi,

    I have a model built in OpenAccess that I query primarily in Linq, going against SQL Azure.

    Although I have a decent model, I built a small "QueryResult" class that I use primarily in a couple of cases where I return data from a stored procedure, rather than through Linq.  This class primarily exists because I'd rather not deal with anonymous types at this time.

    Here's the class.  As you can see it just has a "holder" field for Item, which is whatever I'm querying, and the Amount of sales represented by that item.

        public class QueryResult : IComparable<QueryResult>  
        {  
            public string Item { getset; }  
            public decimal Amount { getset; }  
     
            public int CompareTo(QueryResult other)  
            {  
                return -Amount.CompareTo(other.Amount);  
            }  
        } 

    So my stored proc queries all work fine.  And this Linq query works fine as well:

            public List<QueryResult> TransactionsBySource(int accountId, DateTime start, DateTime end)  
            {  
                var transactions = from t in Scope.Extent<Transaction>()  
                                   where t.AccountId == accountId && t.TransactionDate > start && t.TransactionDate < end  
                                   group t by t.Source into g  
                                   select new QueryResult() { Item = g.Key, Amount = g.Sum(s => s.PaymentGross) };  
     
                List<QueryResult> results = transactions.ToList();  
                results.Sort();  
     
                return results;  
            } 

    However, this Linq query fails.  You'd think it would be a meaningless change, but apparantly changing the Key to ToString() is wrong.

            public List<QueryResult> TransactionsByYear(int accountId)  
            {  
                var transactions = from t in Scope.Extent<Transaction>()  
                                   where t.AccountId == accountId  
                                   group t by t.Year into g  
                                   select new QueryResult() { Item = g.Key.ToString(), Amount = g.Sum(s => s.PaymentGross) };  
                return transactions.ToList();  
            }  
     

    It generates this error:

    Unable to cast object of type 'Telerik.OpenAccess.RT.QueryBuilderImp' to type 'OpenAccessRuntime.DataObjects.query.Node'.

    Is this a procedure that I just should not be doing, or is this an issue with OpenAccess?

    Thanks,

    Chris
  2. Chris Williams
    Chris Williams avatar
    63 posts
    Member since:
    Oct 2004

    Posted 16 Dec 2009 Link to this post

    Well, I solved this my making my return class generic.  Then I can avoid making the ToString() call in the Linq query:

        public class QueryResult<T> : IComparable<QueryResult<T>>   
        {  
            public T Item { getset; }  
            public decimal Amount { getset; }  
     
            public int CompareTo(QueryResult<T> other)  
            {  
                return -Amount.CompareTo(other.Amount);  
            }  
        } 
  3. DevCraft banner
  4. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Dec 2009 Link to this post

    Hi Chris Williams,

    We are happy to see that you have found a solution for the problem.
    In fact this is a known issue of our Linq implementation. It appears when a cast logic is applied inside the projection statement when group by clause is specified. The problem will be fixed in the future release of the product.

    All the best,

    Damyan Bogoev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top