简体   繁体   中英

C# Linq Search by a list into a linq

Objects:

ticketTypeRep = new TicketTypeRepository();
packageRep = new PackageRepository();

The data comes from a this list searched by the user:

var pases = ticketTypeRep.Fetch()
    .Where(x => x.VTP_NOMBRE_PASE.ToUpper().Trim().Contains(searchString.ToUpper().Trim()))
    .Select(x => new { id = x.VTP_COD_TIPOPASE}).ToList();

Im trying to use that data as a list to search every item in another list

var packageWithPase = packageRep.Fetch()
    .Where(x => x.PKG_TICKETTYPE.Trim().Contains("21"))
    .Select(x => new { id = x.PKG_SEQID, name = x.PKG_NAME, state = x.PKG_STATE, details = x.PKG_SEQID, edit = x.PKG_SEQID })
    .OrderBy(x => x.name).ToList();

The thing is that "21" have to be the result of the first list named pases and not a simple string.

Search by a list into a linq I guess.

What you want is to do the comparison other way round like:

passes.Contains(x.PKG_TICKETTYPE.Trim())

Remember this is not String.Contains instead it is Enumerable.Contains So your query would be:

var packageWithPase = packageRep.Fetch()
                        .Where(x => passes.Contains(x.PKG_TICKETTYPE.Trim())
                        .Select(x => new
                        {
                            id = x.PKG_SEQID, 
                            name = x.PKG_NAME, 
                            state = x.PKG_STATE, 
                            details = x.PKG_SEQID, 
                            edit = x.PKG_SEQID
                        }).OrderBy(x => x.name)
                        .ToList();

It's not entirely clear what you want but it looks like you want records where pases contains the PKG_TICKETTYPE value, which would be:

var packageWithPase = packageRep.Fetch()
                                .Where(x => pases.Contains(x.PKG_TICKETTYPE.Trim())
                                .Select...

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