Roberto2790
Roberto2790

Reputation: 25

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.

Upvotes: 1

Views: 179

Answers (2)

D Stanley
D Stanley

Reputation: 152501

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...

Upvotes: 1

Habib
Habib

Reputation: 223187

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();

Upvotes: 1

Related Questions