This is a migrated thread and some comments may be shown as answers.

Returning type from Linq query

2 Answers 329 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris Williams
Top achievements
Rank 1
Chris Williams asked on 16 Dec 2009, 06:49 PM
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 Answers, 1 is accepted

Sort by
0
Chris Williams
Top achievements
Rank 1
answered on 16 Dec 2009, 07:03 PM
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);  
        }  
    } 
0
Damyan Bogoev
Telerik team
answered on 17 Dec 2009, 04:21 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Chris Williams
Top achievements
Rank 1
Answers by
Chris Williams
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or