使用另一个值列表过滤linq

时间:2015-05-04 10:52:20

标签: linq c#-4.0

我有一份客户历史数据列表,其中也包含电话号码。 我需要根据我的电话号码列表过滤历史数据,这是我的代码

历史课

0(1)

我从api call

获取消息历史记录列表
 public class MessengerHistory
    {
        public Int64 ClientId;
        public string FirstName;
        public string LastName;
        public string ChartNumber;
        public string PhoneNumber;
        public string PetName;
        public string Messagetext;
    }

List<string> phonenumber = List<string>{ "12345","45678","35487"}

我想过滤var messageOverview = messengerAppWorkflow.GetMessageOverview(); List<MessageHistroy> mHistory = messageOverview.messages.Select(item => item.clientPhone).ToList(); 值,以使用mHistory返回与phonenumber列表匹配的历史值。

2 个答案:

答案 0 :(得分:2)

你可以加入他们:

var query = from msg in messengerAppWorkflow.GetMessageHistory()
            join phone in phonenumber
            on msg.PhoneNumber equals phone
            select msg;
List<MessageHistroy> mHistory = query.ToList();

另一种less efficient方式正在使用Enumerable.Where + Contains

List<MessengerHistory> mHistory = messengerAppWorkflow.GetMessageHistory()
    .Where(msg => phonenumber.Contains(msg.PhoneNumber))
    .ToList():

答案 1 :(得分:-1)

//这就是你已经拥有的

json_encode($array, JSON_NUMERIC_CHECK); 

//假设messengerHistoryList是你从api获得的列表:

        List<MessengerHistory> reqMessengerHistoryList = new List<MessengerHistory>();

        List<string> phoneNumberList = new List<string> { "12345", "45678", "35487" };

//这就是你需要做的事情:

        List<MessengerHistory> messengerHistoryList = new List<MessengerHistory>();
        MessengerHistory messengerHistory1 = new MessengerHistory
        {
            ClientId = 123,
            FirstName = "Rahul",
            LastName = "Manhotra",
            ChartNumber = "abc",
            PhoneNumber = "12345",
            PetName = "pqr",
            Messagetext = "This is simple"
        };
        MessengerHistory messengerHistory2 = new MessengerHistory
        {
            ClientId = 123,
            FirstName = "Peter",
            LastName = "stuv",
            ChartNumber = "abc",
            PhoneNumber = "34534",
            PetName = "stu",
            Messagetext = "This is hard"
        };
        messengerHistoryList.Add(messengerHistory1);
        messengerHistoryList.Add(messengerHistory2);