C# 多个通用列表- 将它们结合起来?
场景:
我有一个审核的通用列表和一个审核图像的通用列表。这两个列表是根据数据库表编制的。因此,一个 AuditImage 可以进行多次审核。正如您将在下面看到的,表映射到的类在数据库中时通过外键关系“ImageID”连接,但是一旦将数据提取到代码中的列表,就不再存在“物理连接”。
数据库表映射到的类:
public class AuditImage
{
public Guid ImageID { get; set; }
public string LowResUrl { get; set; }
}
public class Audit
{
public Guid AuditID { get; set; }
public Guid ImageID { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public string Comment { get; set; }
}
问题:
我现在想要通过从每个列表中提取数据并将其组合到“Audit.ImageID == AuditImage.ImageID”上来编译“Trail”对象的列表,到一个新列表中。
public class Trail
{
public Guid ImageID { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public string Comment { get; set; }
public string LowResUrl { get; set; }
}
(上面基本上将“LowResUrl”字段与基于相同 ImageID 的每次审核结合起来。)
问题:
我应该如何去做这个!?我曾考虑过使用 foreach 循环和 linq 来创建一个新的跟踪对象列表,但我不太清楚我将如何去做?!
帮助将不胜感激。
Scenario:
I have a generic list of Audits and a generic list of AuditImages. These two lists have been compiled from database tables. As a result of this, ONE AuditImage can have MANY Audits. As you will see below, the classes that the tables map to are joined by a foreign key relationship "ImageID" when they are in the database, however once the data is extracted to lists in code, there is no "PHYSICAL JOIN".
Classes That DB Tables Map To:
public class AuditImage
{
public Guid ImageID { get; set; }
public string LowResUrl { get; set; }
}
public class Audit
{
public Guid AuditID { get; set; }
public Guid ImageID { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public string Comment { get; set; }
}
The Problem:
I now want to compile a list of "Trail" objects by extracting the data from each list and combining it on "Audit.ImageID == AuditImage.ImageID", into a new list.
public class Trail
{
public Guid ImageID { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public string Comment { get; set; }
public string LowResUrl { get; set; }
}
(The above essentially combines the "LowResUrl" field with each Audit based on the ImageID being the same.)
The Question:
How should I go about doing this!? I had thought about using foreach loops and linq to create a new list of trail objects but I can't quite think of exactly how I would go about doing this?!
Help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你可以做
you could do
尝试列表上的 Intersect 扩展方法: http://msdn.microsoft.com/ en-us/library/bb910215.aspx
我还认为你应该检查你的设计并引入一个通用接口 IAudit 或类似的东西。
Try the Intersect extension method on List: http://msdn.microsoft.com/en-us/library/bb910215.aspx
I also think you should review your design and introduce a common interface IAudit or something similar.
这可以工作......
This could work.......
我可能会使用扩展方法,只是因为我不太熟悉 linq:
I would probably use the extension methods, just because I'm not very ease into linq:
这将解决问题..从 var 查询只需添加到循环中的跟踪对象
This will solve the issue.. from var query just add to trail objects in loop
取决于你想要什么:
内连接(如果有
Audit
,则仅显示AuditImage
):左连接(即使没有
AuditImage
,也显示AuditImage
进行审核
):Depends on what you want:
Inner join (only display
AuditImage
if it has aAudit
):Left join (display
AuditImage
even if it does not have aAudit
):至少有两个选择。
首先是做简单的连接:
其次是使用子查询:
如果 ImageID 是唯一的,两者都应该可以正常工作,但我个人更喜欢子查询,因为它看起来比连接更自然。
There are at least two options.
First is do simple join:
Second is to use sub-query:
Both should work fine if ImageID is unique but I personally like sub-query more because it looks more natural than join.