LINQ left join ordered with empty right-items at the end
LINQ left join ordered with empty right-items at the end
我对 LINQ 和 Entity Framework 的了解很少,我在学习的过程中...
我正在尝试编写一个查询,该查询从名为 GroupView
的 视图 中获取信息,并对名为 table 的 table 执行左连接=16=]...该信息随后将由 <asp:Repeater>
使用。
结果集应该有 all 来自 GroupView
的项目,并在开始处加入了项目(按 GroupSequence
[=55 定义的顺序=]) 和末尾的非连接项目(按 GroupView
项目的 Id
定义的顺序)。
即...
[GroupView] | [GroupSequence]
[Id] [Name] [Calc] | [Id] [GroupId] [UserId] [Sequence]
1 Group 1 23 | 1 1 1 3
2 Group 2 34 | 2 2 1 2
3 Group 3 45 | 3 3 1 1
4 Group 4 56
5 Group 5 67
预期结果...
[Id] [Name] [Calc]
3 Group 3 45
2 Group 2 34
1 Group 1 23
4 Group 4 56
5 Group 5 67
如果我执行以下操作,尽管使用 DefaultIfEmpty
,我得到的只是链接到序列的 3 个组。但是页面显示,即使它只有 3 行...
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty()
where item.UserId == 1
orderby item.Sequence
select new { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
如果我执行以下操作,中继器上的 .DataBind
会抱怨...
The entity or complex type 'DevModel.GroupSequence' cannot be constructed in a LINQ to Entities query
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty(new GroupSequence { Id = @group.Id, UserId = 1, Sequence = @group.Id + 1000 })
where item.UserId == 1
orderby item.Sequence
select new { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
基于this question and accepted answer I have also tried using a DTO这样...
class GroupViewSequenceDTO
{
public int Id { get; set; }
public string Name { get; set; }
public int? Calc { get; set; }
}
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty(new GroupSequence { Id = @group.Id, UserId = 1, Sequence = @group.Id + 1000 })
where item.UserId == 1
orderby item.Sequence
select new GroupViewSequenceDTO { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
但是还是报同样的错误(无法构造)
问题...
我如何编写此查询才能使转发器显示所有 5 行,前 3 行按顺序显示,最后 2 行添加?我做错了什么?
您需要将过滤器移动到左连接条件中,因为当 item
为 null 时它将为 false。
from @group in context.GroupViews
from seq in context.GroupSequences.Where(x => @group.Id == x.GroupId && x.UserId == 1).DefaultIfEmpty()
orderby seq.Sequence ?? int.MaxValue
select new GroupViewSequenceDTO
{
Id = @group.Id,
Name = @group.Name,
Calc = @group.Calc
};
我对 LINQ 和 Entity Framework 的了解很少,我在学习的过程中...
我正在尝试编写一个查询,该查询从名为 GroupView
的 视图 中获取信息,并对名为 table 的 table 执行左连接=16=]...该信息随后将由 <asp:Repeater>
使用。
结果集应该有 all 来自 GroupView
的项目,并在开始处加入了项目(按 GroupSequence
[=55 定义的顺序=]) 和末尾的非连接项目(按 GroupView
项目的 Id
定义的顺序)。
即...
[GroupView] | [GroupSequence]
[Id] [Name] [Calc] | [Id] [GroupId] [UserId] [Sequence]
1 Group 1 23 | 1 1 1 3
2 Group 2 34 | 2 2 1 2
3 Group 3 45 | 3 3 1 1
4 Group 4 56
5 Group 5 67
预期结果...
[Id] [Name] [Calc]
3 Group 3 45
2 Group 2 34
1 Group 1 23
4 Group 4 56
5 Group 5 67
如果我执行以下操作,尽管使用 DefaultIfEmpty
,我得到的只是链接到序列的 3 个组。但是页面显示,即使它只有 3 行...
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty()
where item.UserId == 1
orderby item.Sequence
select new { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
如果我执行以下操作,中继器上的 .DataBind
会抱怨...
The entity or complex type 'DevModel.GroupSequence' cannot be constructed in a LINQ to Entities query
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty(new GroupSequence { Id = @group.Id, UserId = 1, Sequence = @group.Id + 1000 })
where item.UserId == 1
orderby item.Sequence
select new { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
基于this question and accepted answer I have also tried using a DTO这样...
class GroupViewSequenceDTO
{
public int Id { get; set; }
public string Name { get; set; }
public int? Calc { get; set; }
}
from @group in context.GroupViews
join seq in context.GroupSequences on @group.Id equals seq.GroupId into groupSeq
from item in groupSeq.DefaultIfEmpty(new GroupSequence { Id = @group.Id, UserId = 1, Sequence = @group.Id + 1000 })
where item.UserId == 1
orderby item.Sequence
select new GroupViewSequenceDTO { Id = @group.Id, Name = @group.Name, Calc = @group.Calc };
但是还是报同样的错误(无法构造)
问题...
我如何编写此查询才能使转发器显示所有 5 行,前 3 行按顺序显示,最后 2 行添加?我做错了什么?
您需要将过滤器移动到左连接条件中,因为当 item
为 null 时它将为 false。
from @group in context.GroupViews
from seq in context.GroupSequences.Where(x => @group.Id == x.GroupId && x.UserId == 1).DefaultIfEmpty()
orderby seq.Sequence ?? int.MaxValue
select new GroupViewSequenceDTO
{
Id = @group.Id,
Name = @group.Name,
Calc = @group.Calc
};