LINQ - Left Join 3 Observed Characters

I have three classes defined as follows:

public class MaterialByOperator
{
    public int IdOperator{ get; set; }
    public int IdMaterial { get; set;}
}

public class Material
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class AssignedOperator
{
     public int idOperation { get; set; }
     public int idOperator { get; set; }
}

IdMaterialin MaterialByOperatoris "ForeignKey" for Material. Attitude One to Many.

IdOperatorin MaterialByOperatoris the "ForeignKey" for AssignedOperatorin One to One.

Then I define this 3 ObservableCollection:

public ObservableCollection<Material> Materials;
public ObservableCollection<MaterialByOperator> MaterialsXOperator;
public ObservableCollection<AssignedOperator> AssignedOperators;

I want to get the names of operators who do not have any materials. Now I will do this:

var mate = MaterialsXOperator.GroupBy(x => x.idOperator); //Group materials by operatorId
//left join assignedOperators with the grouped materials
var opeasigmate = AssignedOperators.GroupJoin(mate, oper => oper.idOperator, 
                   grupo => grupo.Key, (oper, grupo) => new { oper, grupo });
var operWithoutmate = opeasigmate.Where(x => x.grupo.Count() == 0); 

What do I want to know, since my LINQ knowledge is not very wide (believe it or not, I have forbidden this from my work for many years) is there any simplest way to archive what I want? As I said, my solution works, but I would like to see other points of view that, I hope, will be studied, by the way.

+4
1

Any :

var operWithoutmate = AssignedOperators
    .Where(ao => !MaterialsXOperator.Any(mo => mo.IdOperator == ao.idOperator);

join , . x.grupo.Count() == 0 !x.grupo.Any(). , GroupBy , :

var operWithoutmate = AssignedOperators
    .GroupJoin(MaterialsXOperator, ao => ao.idOperator, mo => mo.IdOperator,
        (ao, moGroup) => new { ao, moGroup })
    .Where(r => !r.moGroup.Any())
    .Select(r => r.ao);

, , :

var operWithoutmate = 
    from ao in AssignedOperators
    join mo in MaterialsXOperator on ao.idOperator equals mo.IdOperator into moGroup
    where !moGroup.Any()
    select ao;
+2

All Articles