Create dropdown list of modules for those not already included in associated table using Linq and MVC

Hi:

I have a table of Scos that has the fields Id and Title - e.g. 1 Algebra, 2 History, 3 French, 4 Math etc.  I would like to create a dropdown list in MVC of these Scos that are not already referred to by in the table CourseScos which includes the ScoId as the foreign key.  For example, in the table CourseScos with fields Id and ScoId that has records 1 1, 2 2 - I would hope to generate a dropdown list that shows French and Math.  

What I have so far is as follows:

var exceptionList  = db.CourseScos.Where(s=>s.ScoId>0).Select(s => s.ScoId); //Scos already used by course
            var query = db.Scos
                .Select(e => e.Id)
                .Except(exceptionList);  

           ViewBag.ScoId = new SelectList(query, "Id", "Title");

Open in new window

 

While the list query shows the proper records, it does not include the fields Id and Title that I need to include in the dropdown list.  Any help figuring out how to do this greatly appreciated.

RBS
RBSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

I will assume you have a table that holds the courses, so you could try the following:

var query = db.Scos
    .GroupJoin
    (
        db.CourseScos,
        s => s.Id,
        cs => cs.ScoId,
        (s,cs) => new { Scos = s, CourseScos = cs}
    )
    .Where(x=>x.CourseScos.Count() == 0)
    .Select(x=>x.Scos)

Open in new window


The GroupJoin will give a list of CourseScos for each Scos. With the Where you only get Scos that have no CourseScos.

Giannis

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RBSAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.