0

I need to select only those items from List object where segmentname is one time only e.g. in image which is attached, only return element with SegmentName = "C" as it is only one time in List...

this is sample code which i have tried so far.

var empty = count
  .Where(x => x.Count > 1 & x.SegmentName.Count() == 1)
  .Select(k => new { 
     SegName = k.SegmentName, 
     Value = k.FieldValue, 
     No = k.Count })
  .ToList();

Sample: this is sample

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
J.SMTBCJ15
  • 471
  • 6
  • 20

5 Answers5

1

This might do the trick for you

elements.GroupBy(x => x).Where(y => y.Count() <= 1).Select(z => z.Key).ToList();

This will return the list of all the elements which exists only once

Example

List<string> SegmentName = new List<string>();
SegmentName.Add("A");
SegmentName.Add("A");
SegmentName.Add("B");
SegmentName.Add("B");
SegmentName.Add("C");
List<string> newlist = SegmentName.GroupBy(x => x).Where(g => g.Count() <= 1).Select(y => y.Key).ToList();

output is

"C"

So for your custom object it should be

count.GroupBy(x => x.SegmentName)
     .Where(y=> y.Count()<=1)
     .Select(k => new {
                        SegName = k.SegmentName, 
                        Value = k.FieldValue, 
                        Count = k.Count 
                      })
     .ToList()
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • `GroupBy` should be based on `x.SegmentName` not `x`; for his example – sujith karivelil Dec 15 '16 at 06:26
  • Yeah You are correct @un-lucky but the example I have taken is not the object of custom type but its just string list thus we dont need it here .. yes but in the edit I should make the example according to his need – Mohit S Dec 15 '16 at 06:28
1

Hope that you are looking for something like this:

var result = count.GroupBy(x=>x.SegmentName)
                  .Where(y=> y.Count()==1)
                  .Select(k => new {
                                      SegName = k.SegmentName, 
                                      Value = k.FieldValue, 
                                      No = k.Count 
                                    }).ToList()
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • this worked for me var result = count.GroupBy(x => x.SegmentName) .Where(y => y.Count() == 1) .Select(k => k);.. Thanks buddy.. – J.SMTBCJ15 Dec 15 '16 at 06:42
  • I think the code won't compiled, you cannot access the field `FieldValue` and `Count` after group by the `SegmentName` – Eric Dec 15 '16 at 07:21
0

Give GroupBy a go

var empty = count.GroupBy(x => x.SegmentName)
  .Select(x => x.First())
  .ToList();
user783836
  • 3,099
  • 2
  • 29
  • 34
0

this may help you, check out this one

http://www.dotnettricks.com/learn/linq/sql-joins-with-csharp-linq

SUF3
  • 1
  • 1
0
var count = new [] { 
    new { SegmentName = "A", FieldValue = "", Count = 2 }, 
    new { SegmentName = "A", FieldValue = "ABC", Count = 1 },
    new { SegmentName = "B", FieldValue = "", Count = 2 },
    new { SegmentName = "B", FieldValue = "ABC", Count = 1 },
    new { SegmentName = "C", FieldValue = "", Count = 3 },
};

var result = count
    .GroupBy(x => x.SegmentName)
    .Where(y => y.Count() == 1)
    .Select(z => z.First()) // Need to implicitly get the first item here
    .ToList();

foreach(var item in result)
{
    Console.WriteLine("SegmentName={0} FieldValue={1} Count={2}", item.SegmentName, item.FieldValue, item.Count);
}
Eric
  • 5,675
  • 16
  • 24