开发者

Can I do a EF statement Where( c => c.NameStr == (is element in list<string> ) )?

I want to do a Where statement but check to see if a field m开发者_StackOverflow中文版ember matches an item in a list of strings instead of just a string. My entities are autogenerated from the DB and stored in the .edmx file.

//selectedAgencys is a List<string> 

List<v_MapCrimeData> list = ent.v_MapCrimeData
                               .Where(c => c.AgencyName == (element in list selectedAgencys));


You want to see if the list contains the field, so you can use the Enumerable.Contains

nt.v_MapCrimeData.Where(c => selectedAgencys.Contains(c.AgencyName))

Since you indicated you're using Linq to Entities you might want to try a join.

List<v_MapCrimeData> list = v_MapCrimeData.Join(
            selectedAgencies, 
            c => c.AgencyName,  //key selector for v_MapCrimeData
            a => a,             //key selected for selectedAgencies
            (c, a) => c).ToList(); //result selector (i.e. return the v_MapCrimeData)


Try

List<v_MapCrimeData> list = (from c in ent.v_MapCrimeData 
                            from x in selectedAgencys 
                            where x == c.AgencyName 
                            select c).ToList();


If you need to ignore case,

nt.v_MapCrimeData.Where(c => selectedAgencys.Any(a => a.Equals (c.AgencyName, StringComparison.OrdinalIgnoreCase)) 


.Where(i => listocheckagainst.Contains(i.valuetoBeChecked))

Edit: spent time signing up to SO and someone else answered

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜