-1

From the database i am getting a list of collection which has repetitive data for eg - total length of the List is 4 which has elements like

Item[0].Id  9
Item[1].Id  10
Item[2].id  9
Item[3].id  10

i want the collection to be of length 2 having both the Id as

Item[0].Id  9, 10
Item[1].Id  9,10
Bob.
  • 3,894
  • 4
  • 44
  • 76
New Coder
  • 501
  • 2
  • 6
  • 23
  • 1
    Have a look at [c# - Linq Distinct on particular Property](http://stackoverflow.com/a/489421/1466627). – Bob. Dec 06 '12 at 12:53
  • 1
    Your question is unclear. What about `Item[2]` and `Item[3]`? What is your criterion for grouping values 9,10 against `Item[0]`? – nakiya Dec 06 '12 at 13:11
  • what exactly i want is dont add the duplicate records in item list – New Coder Dec 06 '12 at 13:18
  • 1
    Very unclear question. Separate code from potential "explanation" or "console output".... – BuZz Dec 06 '12 at 13:47

1 Answers1

0

Your question is a little unclear, so I'm going to take your comment that you don't want duplicates in your item collection as the question. If this is indeed accurate then I can think of two possible solutions: one is to filter duplicates when you get the data from the database, and the other is to enforce this in your c# code.

It won't alter the output to two integers comma separated as you've put in your example, but judging from your later comments this isn't what you're trying to do.

SQL Solution You could use distinct if you're using sql to retrieve the data, for example:

select id from aTable

becomes:

select distinct id from aTable

C# Collection Solution You could use the inbuilt functions of an arraylist, for example, to prevent duplicates:

var data = new int[4];
data[0] = 9;
data[1] = 9;
data[2] = 10;
data[3] = 10;

var item = new ArrayList();

foreach (int i in data)
    if (!item.Contains(i)) item.Add(i);

I hope one of these solves your problem! Best of luck!

LED
  • 56
  • 3