score:1

Accepted answer

Give this a go:

var vendorId = 1;
var result =
    _context
        .Colours
        .Where(x => x.VendorId == null || x.VendorId == vendorId)
        .OrderByDescending(x => x.VendorId)
        .GroupBy(x => x.Name)
        .SelectMany(xs => xs.Take(1))
        .OrderBy(x => x.ID)
        .ToList();

With this sample data:

var colours = new []
{
    new { ID = 1, Name = "Purple", VendorId = (int?)null },
    new { ID = 2, Name = "Blue", VendorId = (int?)null },
    new { ID = 3, Name = "Black", VendorId = (int?)null },
    new { ID = 4, Name = "Purple", VendorId = (int?)1 },
    new { ID = 5, Name = "Orange", VendorId = (int?)1 },
    new { ID = 6, Name = "Mauve", VendorId = (int?)2 },
};

I get this result:

result

score:1

This one is working for me and generating expected results:

var idOrNameNotWithId = 
    colours.Where(x => x.VendorId == cid ||
    (!x.VendorId.HasValue && !colours.Where(x => x.VendorId == cid).Select(x => x.Name).ToList().Contains(x.Name)))
    .ToList();

The idea behind it: anything with expect cid (colour ID) or anything that's null but only if, when you take anything with ID the name of the colour is not there.

And the full working method. I borrowed the colour collection definition from @Enigmativity's answer.

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");
    var colours = new[]
    {
        new { ID = 1, Name = "Purple", VendorId = (int?)null },
        new { ID = 2, Name = "Blue", VendorId = (int?)null },
        new { ID = 3, Name = "Black", VendorId = (int?)null },
        new { ID = 4, Name = "Purple", VendorId = (int?)1 },
        new { ID = 5, Name = "Orange", VendorId = (int?)1 },
        new { ID = 6, Name = "Mauve", VendorId = (int?)2 },
    };

    var cid = 2;

    var idOrNameNotWithId = 
        colours.Where(x => x.VendorId == cid ||
        (!x.VendorId.HasValue && !colours.Where(x => x.VendorId == cid).Select(x => x.Name).ToList().Contains(x.Name)))
        .ToList();
        }
    }
}

Related Query

More Query from same tag