Looping through Linq fields

D

David

Hi,

I have a table with many columns.

I have to do a data merge between two rows of data in this table. I need to
transfer most of the items in one row to another row.

I could go through each and every column and transfer, but I am looking to
do it a lot quicker.


using (DataClassesDataContext dc = new DataClassesDataContext())
{

ServiceSheet sht = dc.ServiceSheets.First(a => a.SheetID ==
SecondaryMergeSheet.Text); // Vendor sheet

ServiceSheet oSht = dc.ServiceSheets.First(a => a.SheetID ==
PrimaryMergeSheet.Text); // Operator sheet

// Here is the sort of thing I need to do, but I don't know how.
foreach (LINQ-FIELD fld in Sht)
{
fld.<fieldname> = Sht.<fieldname>;
}

dc.SubmitChanges();
}


Is this possible or is there another way that I can do this?


--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
M

Martin Honnen

David said:
// Here is the sort of thing I need to do, but I don't know how.
foreach (LINQ-FIELD fld in Sht)
{
fld.<fieldname> = Sht.<fieldname>;
}

dc.SubmitChanges();
}


Is this possible or is there another way that I can do this?

I think you would need to use reflection to achieve that.
 
P

Peter Duniho

[...]
// Here is the sort of thing I need to do, but I don't know
how.
foreach (LINQ-FIELD fld in Sht)
{
fld.<fieldname> = Sht.<fieldname>;
}

What is "Sht"? Where did it come from? What type is it? What's a
"LINQ-FIELD"? Why does it make sense to copy a property from "Sht" to the
same-named property in a field of "Sht"? Why would that field of "Sht"
even have the same-named property?

I have a suspicion you're not really asking about properties, but rather
named columns in a data row. But with such a vague and complete code
example, it will be very hard to make valid assumptions and answer the
question.

Pete
 
D

David

Hi,

Sorry, Sht should be sht, which is the query from above. Actually, just
reading it again, it should be sht in the foreach and oSht actually inside
the loop. I couldn't complete the statement in the IDE because I don't know
how to achieve what I am attempting to achieve. That means that there are
likely to be some small errors in the copy.

LINQ-FIELD is part of what I am not sure.

The sample code (which I can't complete, which is why I am asking the
question) shows that there are two queries open from the ServiceSheet table,
sht and oSht.

There are about 80 fields in the ServiceSheet table.

I want to copy a row from the Vendor ServiceSheet (sht) to the Operator
ServiceSheet (oSht).


The foreach loop is intended to get the field names from sht (first part as
to what I am puzzled about).

During the loop, the intention is to copy the value from sht (vendor) to the
oSht (operator) (second part as to what I am puzzled about).

Now, there is probably a simpler way to do this, but the intention here,
when I could get this to work is to put other logic in place to allow all
fields except about 4 to be copied over.

I can see why what I have written could be confusing, but I did attempt to
word it in such a way as to make it understandable. Knowing what I want to
achieve and wording it in such a way as to make it clear, even though I am
not too familiar with LINQ syntax is not such an easy task.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Peter Duniho said:
[...]
// Here is the sort of thing I need to do, but I don't know
how.
foreach (LINQ-FIELD fld in Sht)
{
fld.<fieldname> = Sht.<fieldname>;
}

What is "Sht"? Where did it come from? What type is it? What's a
"LINQ-FIELD"? Why does it make sense to copy a property from "Sht" to the
same-named property in a field of "Sht"? Why would that field of "Sht"
even have the same-named property?

I have a suspicion you're not really asking about properties, but rather
named columns in a data row. But with such a vague and complete code
example, it will be very hard to make valid assumptions and answer the
question.

Pete
 
P

Peter Duniho

Hi,

Sorry, Sht should be sht, which is the query from above. Actually, just
reading it again, it should be sht in the foreach and oSht actually
inside
the loop.

Plain English is often not as descriptive as code. Do you really mean the
code you posted should look like this:
foreach (LINQ-FIELD fld in sht)
{
oSht.<fieldname> = fld;
}

If not, what did you mean?

In any case, if you don't show the type "ServiceSheet", it's not possible
to discuss what one can do with instances of "ServiceSheet". You may or
may not need reflection, as Martin suggests. Preferably not, because
reflection is error-prone and slow. But without knowing more about the
exact types here, it's not really possible to suggest something else.

For that matter, it's not at all clear how LINQ applies at all. There's
not really any such thing as a "LINQ-FIELD" per se. If you have some
reason to believe that your class "ServiceSheet" has some feature to
support LINQ-specific operations, that's even more reason that you need to
show exactly how that type is declared.

Ideally, you will post a concise-but-complete code example that shows
clearly what it is you're trying to do. That doesn't mean posting an
exact copy of all your code. Rather, post a complete, compilable program
that shows the essentials of your scenario, without anything more than the
bare minimum required to illustrate what's involved.

Pete
 
D

David

Hi Peter (and everyone else that is helping),

teh LINQ-FIELD is of a type that I don't know if it was possible with LINQ
or not. This is why I wrote it in uppercase, to highlight what I didn't
know.

If I was doing it from a VBScript perspective, I would do something like...

for each fName in rs.fields

rs2(fName) = rs(fName)

next


This would effectively cycle through all the fields in a record and copy the
value from rs(fName) to rs2(fName).

That is all I was aiming for.


It actually turns out that I had to do it another way (another issue caused
me to change direction), so what I am looking for is academic, but still an
interesting exercise.


--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 

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

Similar Threads


Top