Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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


C#
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
Posted
Updated 14-Mar-18 1:09am
v3
Comments
Maciej Los 9-Mar-18 15:17pm
   
Seems that SelectAll method returns a List(of Analytics), which is not a List(of Employees)...
Amr Mohammad Rashad 9-Mar-18 15:25pm
   
I am giving just an example of what is going on. Indeed the SelectAll method in the code I posted returns List of Analytics. The problem is not what type is being returned the problem is that the result contains a repeated element. For example, if the filter condition should return Analytics1, Analytics2, Analytics3, and Analytics4. It returns Analytics1 repeated 4 times Analytics1, Analytics1, Analytics1, and Analytics1
Maciej Los 9-Mar-18 15:45pm
   
Well, please, next time use "Reply" wigdet if you want to inform me about your reply.
Seems, we misunderstand each other. If Analytics object is a set of objects, your code is working fine, because this is how joins on database level works...
Got it?
Amr Mohammad Rashad 12-Mar-18 17:34pm
   
OK! I will in the next time sorry did not see it :). I know what causes the problem. The problem was in the model configuration. I configured the Analytics model with an composite key, countryid and productid, respectively, the problem is that productid and countryid are repeated, they are not uniquely identify each individual row in the database. As a result the returning result list in general takes the first occurrence that fulfill the condition and repeated it number of times for all other objects that fulfill the condition too within the returned list (i.e., after fulfilling the condition I should have 532 unique object within the list of Analytics, however, I got 532 object within the list but they are not unique it took the first occurrence that fulfill the condition and repeated it 532 times).
Maciej Los 12-Mar-18 17:51pm
   
OK, please, see my answer.
OK, please, post your answer.

1 solution

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...
   
Comments
Maciej Los 14-Mar-18 17:22pm
   
Mark it as a solution (green button) to remove your question from unanswered list.
Richard Deeming 15-Mar-18 15:55pm
   
That's quite a wide key. You might be better off creating a surrogate key instead.
Amr Mohammad Rashad 6-Apr-18 0:30am
   
I depend on another view in creating mine and those columns, together, form a unique identifier for each row within the row set!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900