日期:2014-05-18  浏览次数:21103 次

请教list查询
C# code

    class Program
    {
        static void Main(string[] args)
        {
            List<string> ls = new List<string>(){
                "aa","bb","cc"
            };
            List<people> lp = new List<people>(){
                new people{name="aa",age="1"},
                new people{name="a",age="1"},
                new people{name="bb",age="1"},
                new people{name="dd",age="1"},
            };
        }
        //问题:
        //如何查出lp中的name存在于ls中的people列表?
        //结果: people{name="aa",age="1"},people{name="bb",age="1"},
    }
    public class people
    {
        public string name;
        public string age;
    }



------解决方案--------------------
var query = lp.Where(x => ls.Contains(x.name));
foreach (var item in query)
{
...
}
------解决方案--------------------
探讨
var query = lp.Where(x => ls.Contains(x.name));
foreach (var item in query)
{
...
}

------解决方案--------------------
C# code

 List<string> ls = new List<string>(){
                "aa","bb","cc"
            };
            List<people> lp = new List<people>(){
                new people{name="aa",age="1"},
                new people{name="a",age="1"},
                new people{name="bb",age="1"},
                new people{name="dd",age="1"},
            };
            var namequery = from xx in lp
                            where ls.Contains(xx.name)
                            select xx;
            foreach (people resultname in namequery)
            {
                MessageBox.Show(resultname.name + "  " + resultname.age);
            }