Linq过滤器重复返回数据 [英] Linq filter returned data repeated

查看:69
本文介绍了Linq过滤器重复返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



我试图使用Linq方法过滤对象列表但是,在过滤器执行后,列表返回满足条件的第一个元素在整个列表中重复(即,重复其他元素满足条件的次数)。例如,我返回的员工只有国家ID = 3,我应该有三名员工绿色,蓝色和红色,但是,我只有Blue重复3次蓝色,蓝色,蓝色。



我使用MySql作为我的后备存储,我使用的是Entity Framework和MySqlConnection对象



Greetings,

I am trying to filter a list of object using Linq method Where, however, after filter execution the list returned the first element that fulfilled the condition repeated across the whole list (i.e., repeated the number of times for other elements fulfilling the condition too). For example, I am returning employees only with country id = 3 and I should have three employees Green, Blue, and Red, however, I got only Blue repeated 3 times Blue, Blue, Blue.

I am using MySql as my backing store and I am using Entity Framework with MySqlConnection object

public class Repository<T> : IRepository<T> where T : class
{
    public IEnumerable<T> SelectAll()
    {
        return db.Set<T>().ToList();
    } 
}







public class AnalyticsRepository : Repository<Analytics>
{
    public new List<Analytics> SelectAll()
    {
            return base.SelectAll().ToList<Analytics>();
    }
}







public IEnumerable<Analytics> SelectByCountryAndProduct(string countrycriteria, string productcriteria)
{
  List<Analytics> result = null;

  using (AppDbContext db = new AppDbContext(factory.GetConnection()))
  {
     db.Database.CommandTimeout = 6000;
     analyticsRepository = new AnalyticsRepository(db);
     result = analyticsRepository.SelectAll();
  }

  return result.Where(a => a.CountryId.ToString() == countrycriteria && a.ProductId.ToString() == productcriteria).ToList();
}





我的尝试:



调试并尝试使用虚拟数据的列表上的另一个示例解决方案正常工作



What I have tried:

Debugging and trying another sample solution on a list with dummy data which is worked fine

推荐答案

我替换了



I replaced

public class AnalyticsConfiguration : EntityTypeConfiguration<Analytics>
    {
        public AnalyticsConfiguration()
        {
            HasKey(a => new { a.CountryId, a.ProductId });
        }
    }











With

public class AnalyticsConfiguration : EntityTypeConfiguration<Analytics>
    {
        public AnalyticsConfiguration()
        {
            HasKey(a => new { a.RepresnentativeName, a.DoctorName, a.CustomerCode, a.Specialization, a.ProductName });
        }
    }





所有其他代码声明都相同......



All other code statements are the same...


这篇关于Linq过滤器重复返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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