tags:

views:

41

answers:

3

I have a dataset with two columns as follows;

Prod Value
A    1
A    2
A    3
B    2
B    4
C    1
C    2

So I want all the rows where value is common for Prod, so in the example above only the following would be returned.

A   2
B   2
C   2 

Either LINQ or sql would be helpful

Thanks

A: 
--    DROP TABLE #t2  UNCOMMENT WHEN TESTING...

CREATE TABLE #t2
(
 Prod varchar(50),
 Value int
)

INSERT INTO #t2 VALUES ('A', 1)
INSERT INTO #t2 VALUES ('A', 2)
INSERT INTO #t2 VALUES ('B', 2)
INSERT INTO #t2 VALUES ('B', 4)
INSERT INTO #t2 VALUES ('C', 2)

SELECT 
    DISTINCT
    p1.Prod, 
    p2.Value 
FROM 
    #t2 p1
INNER JOIN 
    #t2 p2 
ON 
    p2.Value = p1.Value AND p1.Prod <> p2.Prod

This is a SQL Server based solution :).

JonH
A: 

You can use the Intersect operator on the second column of the dataset.

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var commonNumbers = numbersA.Intersect(numbersB);

Linq Samples

NebuSoft
Appreciate the reply, you are suggesting grouping by Prod and using intersect to find the matching values?
Dub
+1  A: 

Here is what I came up, I would think there is an easier way but;

        var prodList = (from col in list select col["Prod"]).Distinct().ToList();


        var valueList = from col in list
                        group col by col["Value"] into g
                        where g.Count() == prodList.Count()
                        select g.Key;

Which will give me all of the values I am looking for

Dub