Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have sql code that returns all appointment times that are not used on certain date

select terminRez
from TerminRezervacije t
where not exists(select TerminRezervacijeID
from Rezervacija as r where
t.TerminRezervacijeID=r.TerminRezervacijeID and  r.DatumRezervacije= '2021-01-14')

I managed to recreate it as query with c# LINQ

var termin = from t in db.TerminRezervacije
                        where !db.Rezervacija.Where(r => t.TerminRezervacijeID == r.TerminRezervacijeID
                        && r.DatumRezervacije == mod.DatumRezervacije).Any()
                        select  t.terminRez;

I want to be able to display available appointment times with dropdown list.
Right now i have setup with SelectListItem that loads all appointment times from db
and cant figure out how to implement this linq query to be able to display results of query in my SelectListItem dropdown list.

model.TerminRezervacije = db.TerminRezervacije
            .Select(p => new SelectListItem
                Value = p.TerminRezervacijeID.ToString(),
                Text = p.terminRez


I appreciate any help!!

What I have tried:

I recreated sql query in linq code but cant figure out how to add query results to dropdown list
Updated 16-Jan-21 0:41am

1 solution

Your query can be re-written this way:
var termin = db.TerminRezervacije
        !db.Rezervacija.Any(r => t.TerminRezervacijeID == r.TerminRezervacijeID
            && r.DatumRezervacije == mod.DatumRezervacije))
    //.Select(t=> t)

Then you need to bind the result of above query to DropDownList. For further details, please see:
Populate (Bind) DropDownList from ViewModel in ASP.Net MVC[^]
Create DropDownList using HtmlHelper in ASP.Net MVC[^]
Using the DropDownList Helper with ASP.NET MVC | Microsoft Docs[^]
Share this answer
sasko1 16-Jan-21 16:37pm    
I somehow didnt see query you wrote and went looking through links.
Ended up writing code with SqlDataReader(first link) and it was exactly what i needed
Maciej Los 17-Jan-21 1:45am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900