Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
linq to sql in i have one id (Primary key) as argument and i want to update record those id and return this id row means whole value which is available in table..

thanks in advanced...
Posted
Comments
OriginalGriff 10-Mar-14 5:32am    
And?
What have you tried?
Where are you stuck?
keyur_raval 10-Mar-14 5:38am    
i want to update table using linq to sql.
in which i have one argument (customerId)
and i want to return that customerid row means that row which is updated..
OriginalGriff 10-Mar-14 5:50am    
Yes, I guessed that from the question...
So what part of this is giving you a problem?
keyur_raval 10-Mar-14 5:53am    
public Customer DeleteCustomers(Customer data)
{
LinqToSQL.Customer cl = (from c in _dbHCG.Customers
where c.Isdeleted == false && c.CustomerId == data.CustomerId
select c
).SingleOrDefault();
cl.Isdeleted = true;
_dbHCG.SubmitChanges();
return cl;
}

above code in which return value in problem how to solve i am so help me..
keyur_raval 10-Mar-14 6:16am    
sir please give me fast reply..thanks in advanced... in which i want to updated row return...

C#
var db = new MyDatabase();
var original = db.Users.Find(ObjUser.UserId);
if(original != null)
{
    original.Name = updatedUser.Name;
        original.Email = updatedUser.Email;
    db.SaveChanges();
}
 
Share this answer
 
Comments
keyur_raval 10-Mar-14 6:44am    
i don't understand...but i am write this ..but output not occured..

Customer cr = (from c in _dbHCG.Customers
where c.Isdeleted == false && c.CustomerId == data.CustomerId
select new Customer
{
FirstName = data.FirstName,
LastName = data.LastName,
Address = data.Address,
City = data.City,
Country = data.Country
}).FirstOrDefault();
cr.Isdeleted = true;
_dbHCG.SubmitChanges();
return cr;
Vi(ky 10-Mar-14 6:50am    
I don't know why you are doing in this complex way
You can use my first option or you can use this also

db.Users.Attach(updatedUser);
db.Entry(updatedUser).State = EntityState.Modified;
db.SaveChanges();
keyur_raval 10-Mar-14 7:03am    
but sir in your query how return whole row and i am trying your way but in which find method not available ..so quickly give me nice hint...
Hi keyur_raval597,

I have updated a Table named TL_Cateqory and returned the updated record as my output:
DataClasses1DataContext is my dbml file in which i have dragged my table using server explorer.


C#
DataClasses1DataContext obj1 = new DataClasses1DataContext();
Console.WriteLine(Update Single Record);
var sample = obj1.TL_Categories.Single(sd => sd.CategoryID == 6);
sample.CategoryName = "Hello";
obj1.SubmitChanges();
var output = obj1.TL_Categories.Where(c => c.CategoryID == 6);
Console.WriteLine(output);
foreach (var i in output)
{
 Console.WriteLine(i.CategoryID+i.CategoryName+i.AddedBy);
}
Console.ReadKey();
 
Share this answer
 
Comments
keyur_raval 10-Mar-14 9:39am    
when i am using this query it's return this error:-

Error 1 Cannot implicitly convert type 'HCG.CRM.Data.LinqToSQL.Customer' to 'HCG.CRM.Core.Entities.Customer'

please solve my problem quickly bro....

public Customer UpdateCustomers(Customer data)
{
var cs = (from u in _dbHCG.Customers
where u.Isdeleted == false && u.CustomerId == data.CustomerId
select u).FirstOrDefault();

cs.FirstName = data.FirstName;
cs.LastName = data.LastName;
cs.Address = data.Address;
cs.City = data.City;
cs.Country = data.Country;
cs.CustomerId = data.CustomerId;
_dbHCG.SubmitChanges();

return cs;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900