简体   繁体   中英

LINQ SQL Select from list using partial match

I have a list wbsList containing the current elements:

SS-B23813
SS-B23814

I want the SQL lookup to retreive all wbs elements that starts with those numbers to be listed, so I use this code:

var q =
    from a in MESdb.GetTable<t_SAP_Order>()
    where wbsList.Contains(a.WbsElement) 
    orderby a.WbsElement, a.OrderDescription
    select a;

This results in nothing, because it only shows exact matches. All my wbs'es has a longer string ( SS-B23813-24-1-15-06-100 ) How can I use the list as a partial search criteria?

UPDATE:

When I change the code to Dunth's answer, I get the following error: Local sequence cannot be used in LINQ to SQL implementations of query operators except Contains operator.

I wonder if this error comes because of some error when I try to display the result in a datagrid:

caseGrid.DataSource = q.Select(o => new
                    {
                        Workcenter = o.MainWorkCenter,
                        SO = o.Ordr,
                        Description = o.OrderDescription,
                        SerialNumber = o.SerialNumber,
                        BasicFinish = o.BasicFin
                    }).ToList();

Try this to find where it might not be at the start.

var q = MESdb.GetTable<t_SAP_Order>()
            .Where(a => wbsList.Any(b => a.WbsElement.Contains(b)))
            .OrderBy(a => a.WbsElement)
            .ThenBy(a => a.OrderDescription).ToList();

Contains一个sql IN(...) ,您想要一个sql LIKE这样,所以使用wbList.StartsWith(a.WbsElement)

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