比较两个列表

时间:2015-10-29 22:33:31

标签: c# linq

我管理一个呼叫中心。我正在尝试将未应答的呼叫添加到池中。

我有两个列表,我只从一个表中获取这些列表,因为我的一些属性为null,其中一些不为null。

空值表示业务代表没有接听,非空值表示业务代表接听电话,或稍后再拨打错过的客户端。

简单的部分是从表中获取空值并将它们添加到池中。困难的部分是找出代理是否在将未接来电添加到池之前回叫。

请参阅下面的示例。在示例中,我想从“abc”列表中删除def列表中AgentAnswerTime不为空的项目。

List<UnansweredCallList> abc =
    { PhoneNumber = "123123", AgentAnswerTime = "null", CallTime = "12:30:59" },
    { PhoneNumber = "321321", AgentAnswerTime = "null", CallTime = "12:54:50" }

List<AnsweredCallsList> def = 
    { PhoneNumber = "123123", AgentAnswerTime = "10272015 12:31:00", CallTime = "12:30:59" },
    { PhoneNumber = "321321", AgentAnswerTime = "null", CallTime = "12:54:50" }

我想要这个结果。

List<UnansweredCallList> abc = 
    { PhoneNumber = "321321", AgentAnswerTime = "null", CallTime = "12:54:50" }

我尝试了linq Except

实际代码:

var query = (
    from c in db.calls 
    where c.PhoneNO.Length >= 9 && c.AGENT_Answer_Time == null && c.ClientNO != null && c.Call_Time > start_date && c.Call_Time < end_date 
    select new
       {
           c.PhoneNO,
           c.Call_Time,
           c.AGENT_Answer_Time,
           c.ClientNO,
           c.ROBOP
        }).ToList();

 var query = (
    from c in db.calls 
    where c.PhoneNO.Length >= 9 && c.AGENT_Answer_Time != null && c.ClientNO != null && c.Call_Time > start_date && c.Call_Time < end_date 
    select new
        {
            c.PhoneNO,
            c.Call_Time,
            c.AGENT_Answer_Time,
            c.ClientNO,
            c.ROBOP
         }).ToList();

4 个答案:

答案 0 :(得分:2)

from a in abc 
join d in def on a.PhoneNumber equals d.PhoneNumber
where d.AgentAnswerTime == "null"
select a

答案 1 :(得分:1)

定义您的类,以便AnsweredCallsList扩展UnansweredCallList

public class UnansweredCallList
{
    public string PhoneNumber;
    public string AgentAnswerTime;
    public string CallTime;
}

public class AnsweredCallsList : UnansweredCallList
{
}

实施以下等同性比较器

  public class CallListComparer : IEqualityComparer<UnansweredCallList>
    {
        public bool Equals(UnansweredCallList x, UnansweredCallList y)
        {
            return x.PhoneNumber == y.PhoneNumber && y.AgentAnswerTime != "null" || x.AgentAnswerTime != "null";
        }

        public int GetHashCode(UnansweredCallList obj)
        {
            return obj.PhoneNumber.GetHashCode();
        }
    }

然后,使用LINQ.Except:

    List<UnansweredCallList> abc = new List<UnansweredCallList>(new UnansweredCallList[]
    {
        new UnansweredCallList() {PhoneNumber="123123" , AgentAnswerTime="null", CallTime="12:30:59"},
        new UnansweredCallList() {PhoneNumber="321321" , AgentAnswerTime="null" , CallTime="12:54:50"}
    });

    List<AnsweredCallsList> def = new List<AnsweredCallsList>(new AnsweredCallsList[]
    {
        new AnsweredCallsList() {PhoneNumber="123123" , AgentAnswerTime="10272015 12:31:00", CallTime="12:30:59"},
        new AnsweredCallsList() {PhoneNumber="321321" , AgentAnswerTime="null" , CallTime="12:54:50"}
    });

    IEnumerable<UnansweredCallList> result = abc.Except<UnansweredCallList>(def, new CallListComparer());

    Console.WriteLine(result.Count<UnansweredCallList>());

答案 2 :(得分:0)

这样的事情会起作用吗?

void Main()
{
    List<Call> unanswered = new List<Call>()
    {
        new Call() { PhoneNumber = "123123", AgentAnswerTime = "null", CallTime = "12:30:59" },
        new Call() { PhoneNumber = "321321", AgentAnswerTime = "null", CallTime = "12:54:50" }
    };

    List<Call> answered = new List<Call>()
    {
        new Call() { PhoneNumber = "123123", AgentAnswerTime = "10272015 12:31:00", CallTime = "12:30:59" },
        new Call() { PhoneNumber = "321321", AgentAnswerTime = "null", CallTime = "12:54:50" }
    };

    var result = unanswered.Where(call =>
    {
        var answeredCall = answered.FirstOrDefault(c => c.PhoneNumber == call.PhoneNumber);

        return answeredCall != null && answeredCall.AgentAnswerTime == "null";
    });

    foreach(var r in result)
    {
        Console.WriteLine(r.PhoneNumber);
    }
}

public class Call
{
    public string PhoneNumber { get; set; }
    public string AgentAnswerTime { get; set; }
    public string CallTime { get; set; }
}

答案 3 :(得分:0)

我相信你可以使用Except并传递一个自定义IEqualityComparer,你可以实现它来比较你的实例。这会从第一个列表中删除第二个实例,因为第二个实例中存在语义上相等的对象。