Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ,但未按预期工作_C#_Asp.net_.net_Asp.net Mvc_Linq - Fatal编程技术网

C# LINQ,但未按预期工作

C# LINQ,但未按预期工作,c#,asp.net,.net,asp.net-mvc,linq,C#,Asp.net,.net,Asp.net Mvc,Linq,我有一些方法可以返回过去360天、180天和90天内没有查询过的联系人列表 在过去361天内没有查询过的一个人,也将通过180天和90天的查询返回 我想我可以用一个例外,但那肯定不行 public class Contacto { public int IdContacto { get; set; } public string PrimerApellido { get; set; } public string PrimerNombre { get; set; }

我有一些方法可以返回过去360天、180天和90天内没有查询过的联系人列表

在过去361天内没有查询过的一个人,也将通过180天和90天的查询返回

我想我可以用一个例外,但那肯定不行

public class Contacto
{
    public int IdContacto { get; set; }
    public string PrimerApellido { get; set; }
    public string PrimerNombre { get; set; }
    public string SegundoApellido { get; set; }
    public string SegundoNombre { get; set; }
    public object Telefonos { get; set; }
    public int TipoTelefono { get; set; }
    public int IdEstado { get; set; }
    public DateTime? FechaArchivado { get; set; }
    public DateTime? FechaConsulta { get; set; }
GetOldContacts方法

private static List<Contacto> GetOldContacts(int numberOfDays)
{
    try
    {
        DateTime filter = DateTime.Now.AddDays(-numberOfDays);
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri(ConfigurationSettings.Apiurl);
        HttpResponseMessage response = client.GetAsync("api/ContactosDepurar?FechaInicial="+filter.ToShortDateString()).Result;
        if (response.IsSuccessStatusCode)
        {
            return response.Content.ReadAsAsync<List<Contacto>>().Result;
        }
        else
        {
            System.Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            return null;
        }
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex.Message);
        return null;
    }

}
私有静态列表GetOldContacts(int numberOfDays)
{
尝试
{
DateTime过滤器=DateTime.Now.AddDays(-numberOfDays);
HttpClient=新的HttpClient();
client.BaseAddress=新Uri(ConfigurationSettings.Apiurl);
HttpResponseMessage response=client.GetAsync(“api/ContactosDepurar?Fechainical=“+filter.ToShortDateString())。结果;
if(响应。IsSuccessStatusCode)
{
返回response.Content.ReadAsAsync().Result;
}
其他的
{
System.Console.WriteLine(“{0}({1})”,(int)response.StatusCode,response.ReasonPhrase);
返回null;
}
}
捕获(例外情况除外)
{
系统控制台写入线(例如消息);
返回null;
}
}
我的逻辑除外

IEnumerable<Contacto> contactosDoceMeses = GetOldContacts(ConfigurationSettings.Ciclo3Dias);
IEnumerable<Contacto> contactosSeisMeses = GetOldContacts(ConfigurationSettings.Ciclo2Dias).Except<Contacto>(contactosDoceMeses);
IEnumerable<Contacto> contactosTresMeses = GetOldContacts(ConfigurationSettings.Ciclo1Dias).Except<Contacto>(contactosSeisMeses);
IEnumerable contactosDoceMeses=GetOldContacts(ConfigurationSettings.Ciclo3Dias);
IEnumerable contactosSeisMeses=GetOldContacts(ConfigurationSettings.Ciclo2Dias)。除了(ContactosDocemes);
IEnumerable contactostresses=GetOldContacts(ConfigurationSettings.Ciclo1Dias)。除了(contactosSeisMeses);

问题是第二个查询返回第一个查询的项目,它不应该

Linq的
,除非
通过对象的
相等()比较对象。您需要覆盖
Contacto
Equals
GetHashCode

前面的许多问题解释了如何:

Except
还有一个重载,如果您希望实现一个而不是重写函数,它将接收一个
IEqualityComparer

要指定比较器内联查看:


还有更简单的方法吗?