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 (again!) I can't figure out how to use the UnitOfWork.Remove(Query) call to remove many rows in a single call. I'd like to do something like this: uow.Remove(from field in uow.MyFieldData
But that doesn't work. What's the right syntax?
Thanks! |
|
|
Remove(Query) takes a LightSpeed Query object, rather than a LINQ IQueryable. See the Mindscape.LightSpeed.Querying namespace in the API docs. Here's an example: Query toRemove = new Query { uow.Remove(toRemove); |
|
|
I understand what the API says, but the API docs seem a little light on how to different kinds of queries using that syntax. Can you translate the LINQ query I provided into one using the QueryExpression?
Also, is there any way to convert a LINQ query to a Query object or QueryExpression?
Finally, why? Why wouldn't uow.Remove() accept a LINQ query to express what to remove?
Thank you
Gary |
|
|
Yes, the example query I posted is a direct translation of the LINQ query: Query toRemove = new Query { There isn't a systematic way to convert a LINQ query to a query object but here are some pointers: * The EntityType will be the type of entity in the LINQ "from" clause * The LINQ where clause translates to the QueryExpression * Properties in the LINQ "where" clause translate into Entity.Attribute("propname") * Comparisons and logical operators (e.g. <, ==, &&) translate directly, e.g. Entity.Attribute("Price") < 40 && Entity.Attribute("NumberInStock") > 1000 * You do not need to set any Query object properties other than EntityType and QueryExpression The reason IUnitOfWork.Remove() takes a Query rather than an IQueryable is primarily because the LightSpeed core assembly targets .NET 2.0 and therefore cannot depend on LINQ expressions. Another consideration is that we need to detect and reject
LINQ expressions that would be misleading as delete specifiers e.g.
projections, aggregates, joins/groups. However "delete by IQueryable" is a cool idea so we might try to do this in LightSpeed 3 (though probably not in the first drop) or via an extension method in the LightSpeed LINQ provider -- thanks for the suggestion! |
|
|
By the way, for more examples of query expressions, please see the Sample Queries project in the Samples directory. |
|
|
As I can suggest if you want to use with 100% Linq query style, you can create a Remove function that receive in parameter an object of type 'entities As IEnumerable(Of T)' and that in this function, you loop throw each entity and flag it as deleted. Example: Public Overridable Function Remove(ByVal entities As IEnumerable(Of T)) As Boolean Implements IGenericRepository(Of T).Delete
And the code that use it: Dim unitOfWork As Domain.Models.LightSpeed.TerraLogUnitOfWork
For me, this work great and I have no overhead to loop throw a collection of objects instead of using the overload of the function Remove built-in mindscape that receive a Query object.
|
|