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
|
Hi Guys,
I’ve got two tables Template and TemplateVersion in my DB. TemplateVersion table has an foreign key points to Template table. And Template table has a column (CurrentVersionId) store the TemplateVersion Id to keep track of the current version (no foreign key constraint here to void circular association) I’ve got the following Linq query retrieving the current verion TemplateVersion record for all Template. var templateVersions = from tv in UnitOfWorkScope.Current.TemplateVersions where tv.Template.CurrentVersionId == tv.Id select tv; However, the translated SQL query is a little different than I expected. Instead of Template.CurrentVersionId = TemplateVersion.Id, LightSpeed translate it to Template.CurrentVersionId = Template.Id. And this hold true if I use “QueryExpression = Entity.Attribute("Template.CurrentVersionId") == Entity.Attribute("ID")”. Can someone help me out here? Thanks in advance. SELECT TemplateVersion.Id, TemplateVersion.CreatedBy, TemplateVersion.CreatedOn, TemplateVersion.Description, TemplateVersion.FileData, TemplateVersion.KnowledgeArea, TemplateVersion.ModifiedBy, TemplateVersion.TemplateId, TemplateVersion.Title, TemplateVersion.UpdatedOn, TemplateVersion.VersionNumber FROM TemplateVersion WHERE EXISTS ( SELECT Template.* FROM Template WHERE Template.Id = TemplateVersion.TemplateId AND Template.CurrentVersionId = Template.Id ) |
|
|
We have an issue with predicate clauses with attributes on both sides and involving traversals, where the traversal ("Template.") on the left hand side also gets applied to the right hand side. So LightSpeed interprets your query expression as E.A("Template.CurrentVersionId") == E.A("Template.Id"). This means the LINQ query gets misinterpreted (which is a bug: it should raise a NotSupportedException). In LightSpeed 2.x, there is only limited support for attribute-to-attribute comparisons, and really we'd advise that they be used only within the table being queried. I'll investigate whether we can provide an enhancement to support your query, but I can't make any promises. |
|
|
I've added a partial fix for this issue which will be included in nightly builds dated 12 Aug 2009 and above. The idea is that you will be able to write a special annotation on the right hand side of a query to say "this attribute refers to the table I'm querying, not the table I've traversed to." This annotation is to prefix the attribute name with "^." So for your query you would write: Find<TemplateVersion>(Entity.Attribute("Template.CurrentVersionId") == Entity.Attribute("^.Id")); The ^ tells LightSpeed to use the Id of the TemplateVersion (the queried type), not the Template (the type the LHS has traversed into). This works only using the core API and is not available from LINQ. Please let us know if you run into any problems. |
|