score:3

Accepted answer
var count = 50;

var alreayused = from au in repository.getentity<alreadyused>() 
                 select au.id;

var notused = (from nu in repository.getentity<sample>()
              where !alreadyused.contains(nu.id) && nu.references.count() > 0
              orderby nu.name
              select new customclass
              {
                 cname = nu.name,
                 cid = nu.id
              }).take(count).toarray();

notice that i added "nu.references.count() > 0"

i assume that you setup the association relationship called references correctly in your data model so that there are many "reference" objects in for every sample object.

score:0

you should be able to use the same technique from your alreadyused sample. eg:

var reference = from r in repository.getentity<reference>()
                select r.id;

var notused = (from nu in repository.getentity<sample>()
               where !alreadyused.contains(nu.id)
               && reference.contains(nu.id)
               select new customclass
               {
                   cname = nu.name,
                   cid = nu.id
               }).take(count).toarray();

however, if you do have an association made between the sample table and the reference table, then you should probably use paul's method.

score:0

this should achieve what you are looking for. of course there are many ways to do it. personally i'd write it this way.

var items = (from r in repository.getentity<reference>()
             join s in repository.getentity<sample>()
                 on r.fkid equals s.id
             where !repository.getentity<alreadyused>().contains(s.id)
             orderby s.name
             select new customclass
             {
                 cname = s.name,
                 cid = s.id
             })
            .take(count)
            .toarray();

Related Query