C#多个通用列表<t> - 结合它们?</t>

时间:2009-09-30 09:26:01

标签: c# list generic-list

情境:

我有一个通用的审核列表和一个通用的AuditImages列表。这两个列表是从数据库表中编译的。因此,ONE AuditImage可以拥有许多审计。正如您将在下面看到的,表映射到的类在数据库中由外键关系“ImageID”连接,但是一旦将数据提取到代码中的列表,就没有“PHYSICAL JOIN”。 / p>

DB表映射到的类:

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相同的每个Audit结合起来。)

问题:

我应该怎么做呢!?我曾经考虑过使用foreach循环和linq来创建一个新的跟踪对象列表,但我还不太清楚我将如何做到这一点?!

非常感谢帮助。

7 个答案:

答案 0 :(得分:4)

你可以做到


var trails = from audit in audits
   join image in auditImages on audit.ImageId equals image.ImageId
   select new Trail { ImageID = audit.ImageId, CreatedDate = audit.CreatedDate,
                      CreatedBy = audit.CreatedBy, Comment = audit.Comment,
                      LowResUrl = image.LowResUrl };

答案 1 :(得分:0)

尝试列表上的Intersect扩展方法:http://msdn.microsoft.com/en-us/library/bb910215.aspx

我还认为你应该检查你的设计,并引入一个通用接口IAudit或类似的东西。

答案 2 :(得分:0)

这可能有效.......

public List<JobImageAudit> CombineForAuditTrail()
        {
            var result = from a in auditList
                         join ai in imageList
                         on a.ImageID equals ai.ImageID
                         //into ait // note grouping        
                         select new JobImageAudit
                         {
                             JobID = a.JobID,
                             ImageID = a.ImageID.Value,
                             CreatedBy = a.CreatedBy,
                             CreatedDate = a.CreatedDate,
                             Comment = a.Comment,
                             LowResUrl = ai.LowResUrl,

                         };
            return result.ToList();
        }

答案 3 :(得分:0)

我可能会使用扩展方法,因为我对linq不是很轻松:

IEnumerable<Trail> trailList = auditImageList.Join(
  auditList, 
  auditImageItem => auditImageItem.ImageId, 
  auditItem => auditItem.ImageId,
  (auditImageItem, auditItem) =>  new Trail() 
  {  
    LowResUrl = auditImageItem.LowResUrl,
    ImageID = auditImageItem.ImageId, 
    CreatedDate = auditItem.CreatedDate,
    CreatedBy = auditItem.CreatedBy, 
    Comment = auditItem.Comment,
  });

答案 4 :(得分:0)

这将解决问题..来自var查询只需添加到循环中的跟踪对象

    var query = from auditImage in AuditImageList 
    join audit in AuditList 
on auditImage.ImageID equals audit.ImageID 
select new { ImageID= auditImage.ImageID, CreatedDate = audit.CreatedDate, CreatedBy = audit.CreatedBY, Comment = audit.Comment , LowResUrl = auditImage.LowResUrl }; 

foreach (var trail in query) { //Assign Values to trail object and add to list of trails }

答案 5 :(得分:0)

取决于你想要的东西:

内部联接(仅在显示AuditImage时显示Audit):

var innerJoin = from image in images
                join audit in audits on image.ImageID equals audit.ImageID
                select new { image.ImageID, AuditImageId = audit.ImageID };

左连接(显示AuditImage,即使它没有Audit):

var leftJoin = from image in images
               join audit in audits on image.ImageID equals audit.ImageID
               into auditCats
               from auditCat in auditCats.DefaultIfEmpty(new Audit())
               select new { image.ImageID, AuditImageId = auditCat.ImageID };

答案 6 :(得分:0)

至少有两种选择。

首先是做简单的连接:

var trails = Audits.Join(Images, a => a.ImageID, i => i.ImageID, (a, i) =>
    new Trail
    {
        Comment = a.Comment,
        CreatedBy = a.CreatedBy,
        CreatedDate = a.CreatedDate,
        ImageID = a.ImageID,
        LowResUrl = i.LowResUrl
    });

其次是使用子查询:

var trails = Audits.Select(a =>
    new Trail
    {
        Comment = a.Comment,
        CreatedBy = a.CreatedBy,
        CreatedDate = a.CreatedDate,
        ImageID = a.ImageID,
        LowResUrl = Images.Single(i => i.ImageID == a.ImageID).LowResUrl
    });

如果ImageID是唯一的,那么两者都可以正常工作但我个人更喜欢子查询,因为它看起来比加入更自然。