This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
This exception: Unable to cast object of type 'Mindscape.LightSpeed.Querying.PathExpression' to type 'Mindscape.LightSpeed.Querying.LiteralExpression'. Is thrown from this query (in a repository class): Any advice? |
|
|
For more info, I'm using the latest nightly and I can work around the problem with the following example, but it seems like LightSpeed is having hard time translating my intent. The workaround below is inefficient, since it has to call ToList() in two places and can't be evaluated as a single SELECT. var users = UnitOfWorkScope.Current.Users.Where( |
|
|
Yes, I think you might need to stick with the two-pass solution for now. Just to bring you up to date on what I've found, the issue appears to be with the Contains call. This is why you need that ToList in the second query. However, using ToList creates efficiency issues as it will bring back ALL Account entities and then iterate over their Users collections. You can do this better as: var accounts = from a in UOWS.Current.Accounts This will perform the query server-side and bring back only the single account that matches. Similarly the first query can be rewritten as follows: var user = UOWS.Current.Users again avoiding the ToList(). (I think the semantics of these queries is the same as your original -- apologies if I've transformed them incorrectly. Hopefully they're at least close enough that you can fix them up!) It still requires two SELECTs which I appreciate is one more than you'd like but at least each SELECT will be efficient. We'll continue to investigate a fix or workaround for this. |
|
|
Hi Ivan, Thanks for the tips to help with the efficiency. Have you made any progress on this front? At this stage in my web development I seem to be able to defeat the LINQ provider for just about every query I can think of, since I have quite a few many-to-many collections across my models and use Contains a lot to find results. Daniel |
|
|
Hi Daniel, Could you clarify where many-to-many collections come into this? We don't have any progress at the moment, but if many-to-many associations are involved then I'd like to understand how before I do anything further on it. The reason is that if you are querying through a many-to-many then it may make it harder for us to resolve the problem. On the other hand, if we're lucky then it may open up a workaround. Thanks! |
|
|
What I'm running into is a similar expression casting exception when attempting to do the following. Consider a controller class with this action: public IQueryable<Media> GetAccountImages(Account account, int page, int count) What you're looking at here is a many-to-many association (with an explicit through table, as I discovered a bug where if you use Guids and an automatic through association, even if you specify the through association with a Guid Id, the Update Database... method inserts it as an int, but that's another day's forum post) between Account and Media. Media has a value object called MediaType that the query here is discriminating on. The problem however is in the MediaRepository's GetByAccount method, which is here: public IQueryable<Media> GetByAccount(Account account) The Contains clause fails. I can get this query to work but it requires no less than three queries including one that interrogates the through association table, which is unfortunate as I don't get much use out of the graph navigation-based querying, making my experience of the many-to-many support on par with LINQ to SQL if I can't pretend the through table doesn't exist when constructing queries. |
|
|
In addition, it seems I can't even to work around this with the non-LINQ based methods. Surprisingly, I can't laboriously grab a list of GUIDs and perform a QueryExpression using In, without this exception raising: "No mapping exists from object type Mindscape.LightSpeed.Linq.LinqQuery`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type." IQueryable<Guid> throughIds = UnitOfWorkScope.Current.AccountMedias I feel pretty defeated here. Isn't this, by the final workaround, an extremely simple example based query? |
|
|
I'm starting to think that these casting issues have to do with'==' vs. Equals semantics. I was able to get my query to work using this query: var q = If you replace the final line with: var f = q.Where(m => m.MediaType.Name.Equals("ProductImage")); Then it fails with the same problem as the original thread post. I believe it's the reference-equals that might be the cause of the issue.
|
|
|
Confirmed! Changing the original query I posted here, and the subsequent issue, from Equals to '==' fixes the problem. I can now use a single query like so: var query = from u in UnitOfWorkScope.Current.Users I am conditioned to always prefer reference equals in these situations, so I wonder what is causing the disconnect. However, I am happy to be able to get on with it now without compromising on multiple queries. |
|
|
Thanks for the info. You are right that we handle .Equals and == differently -- something for us to look into in future. We'll continue to look at the through associations stuff (Jeremy has already started making some improvements on this), but it sounds like you now have an approach which is working for the queries you need to do. Let us know if there are still areas which we need to prioritise for you. |
|
|
Just to let you know, future nightly builds (beginning 22 June) will contain a fix for the issue with creating GUID identity types in the database for auto through entities. Not your main issue, I realise, but should remove one minor annoyance! |
|