about a query with linq

T

Tony Johansson

Hello!

I have two collections. The first collection consist of two fields which are
named CommandID and Name.
The second collection consist of 3 fields which are named WorksheetRowID and
Parameter and Value
So I want to join CommandID with WorksheetRowID but there is a problem
because they don't have identical format.

The field CommandID in the first collection has this format 99-CCCC
where 99 is numeric and CC is character. For example in the first
collection there could be
CommandID Name
53-CHAPI CC
53-CHABI BC
53-CHASI AP
71-CHAPI BP
71-CHABI TT
71-CHASI TR

Now to the second collection here the collection could be
WorksheetRowID Parameter Value
53 CC 3
53 CD 6
71 BP 7

As you can see here I can't just join on CommandID with WorksheetRowID as it
is but
I want to join the numeric part of CommandID with WorksheetRowID.

So as a summary I want all the rows where the numeric part of CommandID
match WorksheetRowID.

//Tony
 
M

Michael C

Tony Johansson said:
Hello!

I have two collections. The first collection consist of two fields which
are named CommandID and Name.
The second collection consist of 3 fields which are named WorksheetRowID
and Parameter and Value
So I want to join CommandID with WorksheetRowID but there is a problem
because they don't have identical format.

The field CommandID in the first collection has this format 99-CCCC
where 99 is numeric and CC is character. For example in the first
collection there could be
CommandID Name
53-CHAPI CC
53-CHABI BC
53-CHASI AP
71-CHAPI BP
71-CHABI TT
71-CHASI TR

Now to the second collection here the collection could be
WorksheetRowID Parameter Value
53 CC 3
53 CD 6
71 BP 7

As you can see here I can't just join on CommandID with WorksheetRowID as
it is but
I want to join the numeric part of CommandID with WorksheetRowID.

So as a summary I want all the rows where the numeric part of CommandID
match WorksheetRowID.

You could just use the standard string functions to get the string into the
shape you need for the join, eg

x.CommandID.Substring(0, 2) + x.Name equals y.WorksheetRowID + y.Parameter

Michael
 
M

Manu

From c in Collection1
Join d in Collection2
on c.CommandID.SubString(0,2) equals d.WorksheetRowID
select c
 
M

Michael C

Tony Johansson said:
Hello!

How will the linq query look like.

I gave you to part you need but here's the whole thing:

CollectionA.Join(CollectionB,
x => x.CommandID.Substring(0, 2) + x.Name,
y => y.WorksheetRowID + y.Parameter,
(x, y) => new { x, y} )

or

from x in collectionA
join y in collectionB on x.CommandID.Substring(0, 2) + x.Name equals
y.WorksheetRowID + y.Parameter
select new { x, y };

I don't have vs2008 so you might need to modify these a little to get them
working.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top