New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
no way to unpivot data with linqtodb? #957
Comments
@iouris, which SQL do you expect? |
well, any that would unpivot the data, ie select
ca.Name
from Person person
cross apply (
select person.Name1 as name
union all
select person.Name2 as name
) ca |
Maybe i'm missing something but query is obvious var qnames =
People.Select(person => person.Name1)
.Concat(
People.Select(person => person.Name2)
); |
yes, however for a complex - or expensive - query on People this isnt efficient, hence the need for cross apply. |
You are right will check what we can do here. |
@igor-tkachev, from my point it should be implemented in the following way: var qnames =
from person in People
from name in new[] { person.Name1, person.Name2, "someName1", @SomeParam )
select name;
var names = qnames.ToList(); Array should be translated to UNION ALL statements. select
ca.Name
from Person person
cross apply (
select person.Name1 as name
union all
select person.Name2 as name
union all
select "someName1"
union all
select @SomeParam
) ca |
@sdanyliv, thank you for picking this missing feature up so quickly. Do you mind please advising me on how to get a new build of linq2db which includes it, or the best way to get the sources and build it myself. |
You can build from our branch |
@sdanyliv - much appreciated! |
Based on these changes, is it possible to join even complex arrays?
So that you can call something like this:
The where in contains an inner join expression (not completly finished):
|
For me it is better to create temp table with these keys. BulkCopy records to this table and then join. |
The problem with temp table is that you have to pre define the column types, and you have the slow down of a bulk copy (even if it is fast :-) ).. |
It will be slover than temp table solution. Why not create table from CompositeId<> class? |
I will give a try to look at the performance, but the extension should be the same.. |
Bad in temp table solution, that you have to drop temp tables after disposing context. Or keep in mind that they may exist or will be dropped by server after closing session. |
@sdanyliv, thank you for the build instructions. We've been able to use your fix where it comes to single value selection, as in the original issue description: var qnames =
from person in People
from name in
new[] { person.Name1 }
.Concat(new[] { person.Name2 })
select name;
var names = qnames.ToList(); However it doesn't work for a slightly more complicated scenario, ie var qpeople = context.People.Where(/* complex and expensive search */);
var qpeopleAndTheirFriends =
from person in qpeople
from personOrFriend in
new[] { person.Id, person.Name, friendOf = (int?)null }
.Concat(
from friendRelationship in context.FriendRelationships
where friendRelationship.PersonId = person.Id
join friend in context.People on friendRelationship.FriendId equals friend.Id
select new { friend.Id, friend.Name, friendOf = (int?)person.Id }
)
select personOrFriend;
var peopleAndTheirFriends = qpeopleAndTheirFriends.ToList(); Please note that it works as expected in EF (6). We'd greatly appreciate if this could also be looked into. Apologies, I've just realised that this issue doesn't necessarily relate to the original issue I've reported, however it is still an issue for us. |
Hi Guys,
Love linqtodb, however I've come across an issue that I'd like to see if you have a workaround for.
I need to unpivot data, ie
this ends up with an exception: LinqToDB.Linq.LinqException: ''<>h__TransparentIdentifier0.person.FirstName1' cannot be converted to SQL.'
This however works OK on EF 6.
I have tried using an artificial single row table, it dodnt work either.
Is there any way to unpivot data with linq2db?
Thanks
The text was updated successfully, but these errors were encountered: