简体   繁体   中英

how can I compare one int element with a list int

I'm trying to compare my list with one id element in a query, I do this for my school but I don't know how.

List<int> list = (from d in db.Desv 
                  where d.LastDesvId != null 
                  select d.DesvId).ToList();

ViewBag.Desv = new SelectList(db.Desv
                                 .Where(x => x.state.Equals("Proc") && x.DesvId != list)
                                 .OrderBy(y => y.DesviacionId), "Id", "Id");

Contains(T element) returns true if element is in the list, else false. So the code you want is:

            List<int> list = (from d in db.Desv
                          where d.LastDesvId != null
                          select d.DesvId).ToList();

        ViewBag.Desv = new SelectList(db.Desv
                                         .Where(x => x.state.Equals("Proc") && !list.Contains(x.DesvId))
                                         .OrderBy(y => y.DesviacionId), "Id", "Id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM