|
I have Collections.Generic.List of objects so:
Date Reference NetAmount TaxAmount PaidAmount
01/01/2009 0000209 100.00 0.00 100.00
01/03/2009 0000212 120.00 0.00 120.00
01/01/2009 0000209 120.00 0.00 120.00
01/05/2009 0000221 105.00 0.00 105.00
I want to create another Collections.Generic.List but with the items grouped on the Reference field and fields NetAmount, TaxAmount and PaidAmount summed to give:
Date Reference NetAmount TaxAmount PaidAmount
01/01/2009 0000209 220.00 0.00 220.00
01/03/2009 0000212 120.00 0.00 120.00
01/05/2009 0000221 105.00 0.00 105.00
Can I do this with LINQ or some other way please?
(As there may be another way of doing this, other than LINQ, I ddin't post this is the LINQ section of the forum. Apologies if it is misposted)
|
|
|
|
|
|
Is there a .Net function which will create a new instance of an object with all of the object's data but not just copy the reference to the original object? What I need to do is copy an object and alter the data in the copied object without altering the original object.
I have read about the MemberwiseClone() function, but the objects I wish to copy (user defined objects) don't seem to support this method.
Any help is appreciated.
|
|
|
|
|
Nevermind, think I have found the answer by implementing the ICloneable interface. However, if anyone has any other ideas then great, let me have em!
|
|
|
|
|
Different ways fo cloning[^]
Every approach has their advantage/disadvantage. Take your pick
Edit: Fixed the link
modified on Wednesday, February 18, 2009 9:41 AM
|
|
|
|
|
OK, thanks for your reply. But unfortunately that page isn't loading for me. Do I need to register before I can view it?
I do actually have a solution now in place using serialization which seems to work well. Not sure yet of any implications of using this method but I'm sure I will soon find out!
|
|
|
|
|
the class has to cooperate, e.g. by providing a kind of copy constructor, a Clone() method, etc.
You do not really need to declare inheritance from or implementation of ICloneable for that, but once you have a Clone() method, you can as well make it known to the world.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
i m a starter to vb.net ..... i m using access database...... wenever i try to update databse using sql commands............ either it doesnt get updated..........smtymes it does on sm values.......or it enter 0 in database fields...
??????whether i use oledb parameters or direct values ......situation remains same??
|
|
|
|
|
Could you please post the code that you're using to perform the update?
Just a remark from a grumpy old programmer; try and write "real" sentences, with full words (no abbrvs.) and limit the number of dots. It makes your text more readable, which causes it to get read more, which in turn leads to more answers.
..and that's what you want after all, answers
I are troll
|
|
|
|
|
Well my friend below is right.We will never know what is the Problem until you show us the code you have written.
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
|
|
|
|
|
am i writing update statement wrong.when i take values direct from textboxes i write:
Dim cmd1 As New OleDbCommand("update table2 set grade= ' " & TextBox1.Text & " ' and stream= ' " & TextBox2.Text & " 'where name = ' " & TextBox3.Text & " ' ", con)
con.Open()
cmd1.ExecuteNonQuery()
con.Close()
cmd1.executenonquery returns 1 bt grade remains same in database while stream is reset to 0?????
shud i use oledb parameters,,,,,,tel me hw???
|
|
|
|
|
You must start using Parameters. Your Code is untidy
try something like this
Public int Update_Data(String Grade,String Stream ,String Name )
{
con =new OleDbCommand(strcon);
cmd1 = new OleDbCommand();
cmd1.CommandText = "Update table2 set grade=? And Stream = ? where Name = ?";
cmd1.Connection = con;
cmd1.Parameters.Add("@Grade",SqlDbType.NVarChar, 63).Value = Grade;
cmd1.Parameters.Add("@Stream ",SqlDbType.NVarChar, 63).Value = Stream ;
cmd1.Parameters.Add("@Name ",SqlDbType.NVarChar, 63).Value = Name ;
int Results = 0;
try
{
Con.Open();
Results = cmd1.ExecuteNonQuery();
}
catch(SqlException)
{
throw;
}
finally
{
if (con != null)
{
con.Close();
}
}
return Results ;
}
Try this , it might not be Perfect but now if there a Problem its easy to track.
Note that i wrote it here , so dont paste in VS and expect it to work, am just giving you an idea
Hope it help
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
modified on Tuesday, February 24, 2009 7:13 AM
|
|
|
|
|
but u didnt tell me where to take the values from????......
i m using acess database......
Try
Dim i As Integer
Dim a10, a11, a12 As OleDbParameter
Dim cmd1 As New OleDbCommand("update table1 set stream= @a11,grade=@12 where stdid =@10", con)
a10 = cmd1.Parameters.Add("@a10", OleDbType.Integer, 32)
a10.Value = TextBox4.Text
a11 = cmd1.Parameters.Add("@a11", OleDbType.VarChar, 255)
a11.Value = TextBox5.Text
a12 = cmd1.Parameters.Add("@a12", OleDbType.VarChar, 255)
a12.Value = TextBox6.Text
con.Open()
i = cmd1.ExecuteNonQuery()
con.Close()
Label1.Text = i
Catch ex As Exception
Label1.Text = ex.Message.ToString()
End Try
if i put direct values ie stdid = 1 (which z the primary key)den only it gets updated
and strange updation. stream instead of taking value frm textbox5 takes value from textbox4???
and its nt givin ane exception???
|
|
|
|
|
Good Morning parkash_C
The ?? that you see is the Parameters that are unknown. So in OLEDB we do things differently , we dont use a "@" as Parameter but we use the "?" to show that the Value is not yet known but it will be known at runtime. So your Update Statement is fine the way i wrote it for you.
The Other reason your code confuses you , is because its not a function. You just wrote the code, that is why you will not understand why it picks the wrong value. i have written an example code for you but you choose to disregard it
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
|
|
|
|
|
do u think dere cn be problem in Access database cz i have used the same way before nd it worked
|
|
|
|
|
You connection looks fine , until you get an Error that is related to connection, but at the moment you said you are trying to Update but the changes does not take effect. So your connection is not a problem
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
|
|
|
|
|
thanks..i kinda figured out the prob.
its dat i ws using numeric key bt nt converting textbox string into integer..it wrks fyne if key is string
bt i m nt able to find a conversion
i m using CINT nd Val on primary key
bt it updates each field with the primary key???
can u sugest me on conversion???
Thanks!!!plz reply soon
|
|
|
|
|
Try something like this
Convert.ToInt32(TextBox1.Text);
Hope it help.
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
|
|
|
|
|
ya it cnverted it
bt chek dis one out
Try
Dim i As Integer
Dim a10, a11 As OleDbParameter
Dim cmd2 As New OleDbCommand("update table1 set stream= @a11 where stdid= @a10", con)
a10 = cmd2.Parameters.Add("@a10", OleDbType.Integer, 32)
a10.Value = Convert.ToInt32(t5.Text)
a11 = cmd2.Parameters.Add("@a11", OleDbType.VarChar, 255, "stream")
a11.Value = t6.Text
con.Open()
i = cmd2.ExecuteNonQuery()
con.Close()
Label8.Text = i
Catch ex As Exception
Label1.Text = ex.Message.ToString()
End Try
if i take stream value from textbox then it wrks....so no prob with the key firld
bt @a11 with stream is not wrking...
i think sm think is wrong the way i took oledb parameters
plz chek it
|
|
|
|
|
Make sure that the Data type and Dimension Mach your Table
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
www.ITS.co.za
|
|
|
|
|
|
No! work it out yourself. It's your project not ours
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|
|
well, thanks for the reply.
)
|
|
|
|
|
Its your project, not ours. You do it.
..and given the question, a pretty poor choice of user name, as you clearly aren't
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
This sounds much like a homework assignment. That's why you're getting the answers you are.
And they're right, even if it's not homework. If someone else writes the code for you, it's not your work you're turning in and you don't deserve to get the credit for it. And, you just doing yourself a diservice because you won't be prepared for either the next class, or the next project assignment you get.
|
|
|
|