-4

I want to filter duplicate id from a list using **linq, here is the code:**

foreach (var item in _VMReturnStock.scmDistReturnDetails.ToList())
{ 

}

I have ids in scmDistReturnDetails, what should I do in this case? And on the basis of duplicate id I have to set model state false.**

Hashir Malik
  • 798
  • 2
  • 9
  • 27
Farruk
  • 31
  • 1
  • 9

7 Answers7

2

You can use MoreLinq by Jon Skeet (Nuget).

It offers the method "DistinctBy".

foreach (var item in _VMReturnStock.scmDistReturnDetails.DistinctBy(d=>d.Id).ToList())
        { 

        }

DistinctBy checks the return value of the lambda for uniqueness, but it returns the original object.

Tormod
  • 4,551
  • 2
  • 28
  • 50
  • i have to store that ids in a variable and if ids count > 1 than i have to set model state false. i dont need distinct ids – Farruk Feb 01 '16 at 09:29
  • I thought the original question was to filter out records with duplicate id...? What do you mean by "setting the model state false"? – Tormod Feb 01 '16 at 09:33
  • yes filter out duplicate ids and if there is any duplicate id available in list i have to set model state false so user cant save data – Farruk Feb 01 '16 at 09:36
1
var list = _VMReturnStock.scmDistReturnDetails.Select(x=> x.ID).Distinct();
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • For my needs, this is the most useful answer here, give me a list (or count) of everything minus the duplicates. – Josh Jan 06 '21 at 18:19
1
List<int> list = new List<int>() { 1, 2, 5, 3, 1, 2, 3, 4, 5, 6 };

var dups = list.GroupBy(i => i).Where(i=>i.Count()>1).Select(i=>i.Key);

foreach (var k in dups)
  Console.WriteLine(k.Key);
Robert McKee
  • 21,305
  • 1
  • 43
  • 57
0

If you want to achieve a solution with existing LINQ operators, one solution is to GroupBy the ID, and then selecting the first element of each group. If the data source is a LINQ provider, then chances are that the heavy lifting will be done server side.

foreach (var item in _VMReturnStock.scmDistReturnDetails.GroupBy(d=>d.Id).Select(grp=>grp.First()).ToList())
    { 

    }
Tormod
  • 4,551
  • 2
  • 28
  • 50
  • 1
    Please don't post multiple answers. Merge it with your other answer and remove this – Tseng Feb 01 '16 at 09:39
  • 2
    They are two different answers. http://meta.stackexchange.com/questions/60445/is-it-ok-to-post-multiple-answers-to-a-question – Tormod Feb 01 '16 at 09:47
0

I suppose your IDs is int and ModelState default value is true so you can do it like this

        int? uniqueID = _VMReturnStock.scmDistReturnDetails.FirstOrDefault();

        foreach (var item in _VMReturnStock.scmDistReturnDetails.ToList())
        {
            if (item != tmp)
            {
                _VMReturnStock.ModelState = false;
                uniqueID = null;
                break;
            }
        }

If your IDs is not distinct so model state will set to false and uniqueID (that ou gonna return r stored it some where) will not has a value.

Natechawin
  • 316
  • 1
  • 5
0

Your question is not clear at all, and "to set model state false" is a mystery, but based on one of your comments, I think you want to do something like this.

        foreach (var item in scmDistReturnDetails.GroupBy(x=>x.id))
        {
           if (item.Count() > 0) 
             {
               //add item.Key, the ID, into a variable where you can "set model state false"
             }
        }
Tyress
  • 3,573
  • 2
  • 22
  • 45
0

Try this:

List<int> list = new List<int>() { 1, 2, 5, 3, 1, 2, 3, 4, 5, 6 };

            var info = list.GroupBy(i => i).ToDictionary(i => i.Key, i => i.Count());

            foreach (var k in info.Where(k => k.Value > 1))
                Console.WriteLine(k.Key);
shadow
  • 1,883
  • 1
  • 16
  • 24
  • While this would work, it's inefficient if you have large numbers of records that don't have duplicates. You will pull all records from the datasource, even if they don't have any duplicates. See my answer for a better approach. – Robert McKee Feb 01 '16 at 18:06