I have this query:
var rights = from gu in db.GroupUsers
join g in db.Groups on gu.GroupId equals g.GroupId
join gr in db.GroupRights on g.GroupId equals gr.GroupId
join r in db.Rights on gr.RightId equals r.RightId
where gu.UserId == userId && g.IsDeleted == false
select r;
It gets translated to:
SELECT [t0].[Name] AS Name1, [t0].[RightId] AS RightId1
FROM [dbo].[GroupUsers] AS t1
INNER JOIN [dbo].[Groups] AS t2
ON ([t1].[GroupId] = [t2].[GroupId])
INNER JOIN [dbo].[GroupRights] AS t3
ON ([t2].[GroupId] = [t3].[GroupId])
INNER JOIN [dbo].[Rights] AS t0
ON ([t3].[RightId] = [t0].[RightId])
WHERE (([t1].[UserId] = 3345) AND ([t2].[IsDeleted] = 0))
This is still ok, but in .NET my SubSonic objects are all empty. So the query returns 15 objects, but Name and RightId are an empty string and -1.
Can this have anything to do with the fact that Name is returned as Name1 and RightId is returned as RightId1?
I'll have a look into the source code of SubSonic3 to find something.
SubSonic3 Source Code: the query does get translated to the sql above. Because of the Name1 and RightId1, the Load in Database.cs doesn't work properly. The line :
currentProp = cachedProps.SingleOrDefault
(x => x.Name.Equals(pName, StringComparison.InvariantCultureIgnoreCase));
doesn't find the currentProp because the currentProp is Name (and RightId) and not Name1 and RightId1. Well, fixing this won't be for today. Maybe someone from SubSonic3 can have a look at this, because it's pretty annoying. I could write a sort of hack into the source code, but it won't be pretty. I guess the translation should be cleaned up so that Name1 and RightId1 are translated back to Name and RightId.
In TSqlFormatter.cs there's next line that adds these AS strings (in method :
protected override Expression VisitSelect(SelectExpression select)
{
...
if (!string.IsNullOrEmpty(column.Name) && (c == null || c.Name != column.Name))
{
sb.Append(" AS ");
sb.Append(column.Name);
}
...
}
If I put the two Appends in comment, then my linq query does work and I do get the right data. But I guess those two lines are there for a reason, but what reason? In which usecase are the two lines necessary?