C#Linq从列表中查询,仅选择其他列表中具有相同ID的记录

时间:2017-11-17 20:12:04

标签: c# linq

我有一个对象列表。我想从列表中选择项目并对所选项目执行foreach操作。然后获取具有该站点的数据。

C#Linq从列表中查询以仅选择具有来自另一个列表的相同id的记录

模型

网站列表

public class Site
{
    public string Name{ get; set; }
    public int SiteNo{ get; set; }          
}

public class StudentSite
{
    public string Username{ get; set; }
    public string { get; set; }
    public IList<Site> SiteNo { get; set; }
    public DateTime? DateReceived { get; set; }
    public string firstname { get; set; }
    public string DataEntryUser { get; set; }
    public DateTime? CreatedDate { get; set; }
    public int? Count { get; set; }
}

var user = repository.Get(userid);  //it return me the list of sites 10 sites 

results_temp12 = (from workstation in _entitiesCTRS2.AidsM.OrderBy(c => c.Lab_ID )       
                 where user.User_InvSites.ToList().
                 ForEach(c => c.Inv_Site.SITE_NO
                    .Where( w=> workstation.Site_Number == c.Inv_Site.SITE_NO)
                    .ToList()) 
                    select new 
                    StudentSite
                    {
                        firstname = workstation.First_Name,
                        CreatedDate = workstation.Entered_dt,
                        scanid = workstation.Scan_ID,
                        labid = workstation.Lab_ID,
                        SiteNo = workstation.Site_Number,
                        DateReceived = workstation.Date_Received,
                        DataEntryUser = workstation.Entered_By                                
                    }).ToList();

3 个答案:

答案 0 :(得分:3)

这很简单:

list1.Where(item1 => list2.Any(item2 => item1.Id == item2.Id));

这将为list1list2的所有项目提供。

答案 1 :(得分:0)

令人抓狂的是你想要什么。但是我从你的问题中得到了两种可能性:

// Inner join: Only returns foos where foo.Id exists in bars
var results = from foo in foos
              join bar in bars on foo.Id equals bar.Id
              select foo;

// Gets all foos if it contains at least one element found in another list
var results = from foo in foos
              where foo.Sites.Select(f => f.Id).Any(s => bars.Any(b => s == b.Id))
              select foo;

答案 2 :(得分:0)

where条件更改为使用Any而不是Foreach

results_temp12 = (from workstation in _entitiesCTRS2.AidsM.OrderBy(c => c.Lab_ID)
                          where user.User_InvSites.ToList().Any(c => c.Inv_Site.SITE_NO == workstation.Site_Number)
                          //where user.User_InvSites.ToList().ForEach(c => c.Inv_Site.SITE_NO.Where(w => workstation.Site_Number == c.Inv_Site.SITE_NO).ToList())
                          select new StudentSite
                          {
                              firstname = workstation.First_Name,
                              CreatedDate = workstation.Entered_dt,
                              scanid = workstation.Scan_ID,
                              labid = workstation.Lab_ID,
                              SiteNo = workstation.Site_Number,
                              DateReceived = workstation.Date_Received,
                              DataEntryUser = workstation.Entered_By
                          }).ToList();

您可以在Site_No上使用Join并转换结果。

results_temp12 = (from workstation in _entitiesCTRS2.AidsM.OrderBy(c => c.Lab_ID)
                          join user_site in user.User_InvSites on workstation.Site_Number equals user_site.SITE_NO
                          select new StudentSite
                          {
                              firstname = workstation.First_Name,
                              CreatedDate = workstation.Entered_dt,
                              scanid = workstation.Scan_ID,
                              labid = workstation.Lab_ID,
                              SiteNo = workstation.Site_Number,
                              DateReceived = workstation.Date_Received,
                              DataEntryUser = workstation.Entered_By
                          }).ToList();