如何编制列表并从第一个列表中获取recods C# [英] How to compate to list and getting recods from first list C#

查看:65
本文介绍了如何编制列表并从第一个列表中获取recods C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<ABC> lst = new List<ABC>()
{
    new ABC { ID = 1, birthdate = DateTime.Now, name = "Vijay", joinDate = DateTime.Now },
    new ABC { ID = 2, birthdate = DateTime.Now, name = "Sagar", joinDate = DateTime.Now },
    new ABC { ID = 3, birthdate = DateTime.Now, name = "Dhaval", joinDate = DateTime.Now },
};

List<string> availablefield = new List<string>();
availablefield.Add("ID");
availablefield.Add("birthdate");
availablefield.Add("name");





我尝试了什么:



如何比较两个列表和获取数据从第一个列表,但只获取传递到第二个列表的数据。



我属于这种类型的输出



ID = 1,birthdate = DateTime.Now,name =Vijay

ID = 2,birthdate = DateTime.Now,name =Sagar



What I have tried:

how to compare two list and getting data from first list but only getting data from which pass to second list.

I belongs this type of output

ID = 1, birthdate = DateTime.Now, name = "Vijay"
ID = 2, birthdate = DateTime.Now, name = "Sagar"

推荐答案

使用 LINQ(语言集成查询) [ ^ ]你可以更轻松地完成

using LINQ (Language-Integrated Query)[^] you can do it easier
List<string> availablefield = new List<string>();
       availablefield.Add("Vijay");
       availablefield.Add("Sagar");

       lst = lst.Where(k => availablefield.Contains(k.name)).ToList();










// using foreach loop
           List<ABC> lstOutput = new List<ABC>();
           foreach (ABC item in lst)
               if (availablefield.Contains(item.name))
                   lstOutput.Add(item);


这篇关于如何编制列表并从第一个列表中获取recods C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆