没有结果时的 LINQ to SQL 连接
给定以下数据库结构 替代文本 http://dl.dropbox.com/u/26791/tables.png< /a>
我正在尝试编写一个 LINQ 查询,该查询将返回按与其关联的标签分组的图像。到目前为止,我已经得到了这个:
var images = from img in db.Images
join imgTags in db.ImageTags on img.idImage equals imgTags.idImage
join t in db.Tags on imgTags.idTag equals t.idTag
where img.OCRData.Contains(searchText.Text)
group img by new { t.TagName } into aGroup
select new
{
GroupName = aGroup.Key.TagName,
Items = from x in aGroup
select new ImageFragment()
{
ImageID = x.idImage,
ScanDate = x.ScanTime
}
};
效果很好。但是,我还想返回一组“(未标记)”或其他内容中没有任何与之关联的标签的图像。如果不为每个图像插入默认标签,我无法理解如何做到这一点,这似乎通常不是一个很好的解决方案。
Given the following database structure
alt text http://dl.dropbox.com/u/26791/tables.png
I'm trying to write a LINQ query that will return images grouped by tags it's associated with. So far I've got this:
var images = from img in db.Images
join imgTags in db.ImageTags on img.idImage equals imgTags.idImage
join t in db.Tags on imgTags.idTag equals t.idTag
where img.OCRData.Contains(searchText.Text)
group img by new { t.TagName } into aGroup
select new
{
GroupName = aGroup.Key.TagName,
Items = from x in aGroup
select new ImageFragment()
{
ImageID = x.idImage,
ScanDate = x.ScanTime
}
};
Which works great. However, I also want to return Images that do not have any tags associated with them in a group of "(Untagged)" or something. I can't wrap my head around how I would do this without inserting a default tag for every image and that seems like generally not a very good solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果在没有对应标签记录的情况下想要图像记录,则需要执行
图像标签表上的外部联接。
If you want image records when there are no corresponding tag records, you need to perform an
outer join on the image tags table.
这有点棘手,但如果您能够实例化新的
ImageTag
和Tag
实例以供 linq 使用,您可以在一个大型查询中完成此操作。本质上,当您进行外连接时,必须将into
关键字与DefaultIfEmpty(...)
方法结合使用来处理“外连接间隙” (例如,当典型的 SQL 左外连接中连接键的右侧为空时)。希望上面的内容能够编译,因为我没有您的确切环境,我使用自己的数据类型构建了我的解决方案,然后将其转换为您的问题的描述。基本上,关键部分是额外的
into
和DefaultIfEmpty
行,它们本质上有助于将额外的“行”添加到内存中的大规模连接表中(如果您在考虑)传统的sql意义。但是,有一种更具可读性的解决方案,不需要在内存中实例化 linq 实体(您必须自己将其转换为您的环境):
希望这是有道理的。
当然,您始终可以将应用程序设置为默认使用“未标记”来标记所有内容,或者执行一些更简单的 LINQ 查询来创建 ImageTag 表中不存在的图像 ID 列表,然后进行联合或其他操作。
It's a little tricky, but you can do it in one big query if you have the ability to instantiate new
ImageTag
andTag
instances for linq to work with. Essentially, when you're doing an outer join, you have to use theinto
keyword with theDefaultIfEmpty(...)
method to deal with the "outer join gaps" (e.g., when the right side of the joined key is null in a typical SQL left outer join).Hopefully the above compiles since I don't have your exact environment, I built my solution using my own data types and then converted it to your question's description. Basically the key parts are the extra
into
andDefaultIfEmpty
lines that essentially help add the extra "rows" into the massively joined table that's in memory if you're thinking about it in the traditional sql sense.However, there's a more readable solution that doesn't require the in memory instantiation of linq entities (you'll have to convert this one yourself to your environment):
Hope that makes sense.
Of course, you can always just set your application to tag everything by default w/ "untagged" or do some much simpler LINQ queries to create a list of image id's that are not present in your ImageTag table, and then union or something.
这就是我最终所做的。我还没有真正检查过这会生成什么样的 SQL,我猜它可能不太漂亮。我认为我最好自己做几个查询并汇总这些内容,但无论如何这都是有效的:
Here's what I ended up doing. I haven't actually checked what kind of SQL this is generating yet, I'm guessing that it's probably not exactly pretty. I think I'd be better off doing a couple queries and aggregating the stuff myself, but in any case this works: