0

i´m tryin to obtain data from one list that not in another list, the 2 list have the same data type, the two list are a list of Articles whith diferent data:

public class Articles
{
    public string ArticleName{ get; set; }
    public string ClientName { get; set; }
    public DateTime Date { get; set; }
    public string ProviderName { get; set; }
    public string Seller{ get; set; }
    public string ArticleCode { get; set; }
    public float Price { get; set; }
    public float Stock { get; set; }
    public int MiniumUnit { get; set; }
}

List<Articles> LstLowRotation=new List<Articles>();
List<Articles> LstVeryLowRotation=new List<Articles>();

and then i add data in each list(some are differents) and then:

 LstVeryLowRotation = LstVeryLowRotation.Where(x => !lstLowRotation.Any
 (z => z.ArticleCode == x.ArticleCode)).ToList();

but not give me the expected result, they give me more articles that is suposed.

Any idea why don´t works?

also i try

LstVeryLowRotation.except(Lst.LowRotation)

I know is that supossed to work, i have tested with only a few data and works fine, but when i add the data from a datareader it does wrong, and the strangest thing is that when i run appears data that is not in one list or the other list, it´s supposed to be impossible!!!

I edited the names to be more clear.

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
Ion
  • 549
  • 1
  • 11
  • 25
  • 5
    https://msdn.microsoft.com/en-us/library/vstudio/Bb908822(v=VS.90).aspx `List.Except` – austin wernli Jul 27 '15 at 17:18
  • 1
    possible duplicate of [Use LINQ to get items in one List<>, that are not in another List<>](http://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list) – PiotrWolkowski Jul 27 '15 at 17:22
  • @PiotrWolkowski Did you note that accepted answer to the question is also what the OP tried – Conrad Frix Jul 27 '15 at 17:24
  • yes i try but dont work that says – Ion Jul 27 '15 at 17:42
  • you have a type mistake: `Articles` and `Article` ? – dovid Jul 27 '15 at 18:19
  • 1
    its work perfect. see http://volatileread.com/utilitylibrary/snippetcompiler?id=26341 – dovid Jul 27 '15 at 18:34
  • @lomed you are right, when i add manually a few data to the list works fine, the problem is when i add all the data from a datareader,perhasp some proble with the data??? – Ion Jul 27 '15 at 19:18
  • 1
    A suggestion that's not really related to the problem, but that can help readability: having 2 objects of the same type which have the same name apart from 1 character can be really confusing, especially to someone who isn't as familiar with your code. For example, names like lista and lista2 are really nondescriptive as to what's in there. Same with x and z for your iterator. renaming those to, say, stockList and shoppingList, and stockItem and shoppingCartItem is much more clear and understandable. – Nzall Jul 27 '15 at 19:38
  • 1
    Try `z.Articulo.Trim() == x.Articulo.Trim()` it's possible that you have some trailing whitespace characters that cause the comparison to fail. – Dzienny Jul 27 '15 at 19:43
  • 1
    Thanks for the suggestions, NateKerkhofs : You have all the reason, its not enought clear, i will change now. @Dzienny intersting, it´s true that some code have white spaces, tomorrow i will try. Thanks to people how help me. – Ion Jul 27 '15 at 20:16
  • If you have solved your own question, please add an answer explaining the resolution. Avoid altering the question to contain the answer. – Paul Turner Jul 28 '15 at 10:52

2 Answers2

2

Try Enumerable.Except() - I think it will do exactly what you want if you add an equality override for the Articles class telling it to compare the Articulo property.

https://msdn.microsoft.com/en-us/library/vstudio/Bb300779(v=VS.100).aspx

tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
  • It is indeed an elegant way to the desired result, **but not an answer** for the question (_Any idea why don´t works?_) . – dovid Jul 27 '15 at 17:23
  • Yes, i probe `lista2=lista2.except(lista)` following the example, but i obtain exact the same result – Ion Jul 27 '15 at 19:14
0

Finally all the people was right, the problema its in the database, the ArticleCode have many strange characters that had to make the comparison does not work.

THANKS TO ALL PEOPLE TO HELP

Ion
  • 549
  • 1
  • 11
  • 25