关于外部联接的Linq查询帮助,针对对象和对象中的列表

时间:2019-02-25 11:00:03

标签: linq

我有以下对象Job和Job sor是通过从XML文件中读取数据来填充的,而Sor是从数据库中填充的。

class Job
{
    public int JobID { get; set; }
    public string DepartmentCode { get; set; }
    public string ClientReference { get; set; }
    public string JobDescription { get; set; }
    public List<JobSor> JobSorList { get; set; }
}

class JobSor
{
    public int JobID { get; set; }
    public string SorUserCode { get; set; }
    public string SorNotes1 { get; set; }
    public string SorNotes2 { get; set; }
}
class Sor
{
    [Key]
    public string code { get; set; }
    public string description { get; set; }
    public string contract { get; set; }
}

我想编写一个linq查询,该查询将向我显示Sor对象中不存在的所有JobSors。

到目前为止,这是我所拥有的,但是我无法引用SorUserCode属性?

var db = new dbContext();

var sor = db.Sors.Where(p => p.contract == "??");

var query =
          from j in jobs
          join p in sor on j.JobSorList.SorUserCode equals p.code into jp
          from a in jp.DefaultIfEmpty()
          select j;

我该怎么做?

1 个答案:

答案 0 :(得分:0)

首先从JobSor列表中获得所有jobs的列表。 然后应用条件Where,其条件SorUserCodeAny列表的code sor值不匹配。

您的查询如下。

var query = jobs.SelectMany(x => x.JobSorList) .Where(x => !sor.Any(y => y.code == x.SorUserCode));