SSC
SSC

Reputation: 311

mapping object in linq (asp.net)

I want to map t and q .how can i do that? (In asp.net)

I want to get t and map it to q for editing the record

public void EdittblUser(tblUser t)
{
    var q = db.tblUsers.SingleOrDefault(u => u.userName == t.userName );
    //mapping here
    dbconnect.submitchange();
}

some thing like this :

 var user = 
    dbconnect.tblUsers.SingleOrDefault(u => u.userName == _VOUser.Username);

if (user == null)
    // throw exception

Mapper.Map(tbluser, user);
dbconnect.SubmitChanges();
return true;

Upvotes: 0

Views: 54

Answers (1)

Christos
Christos

Reputation: 53958

Try this one:

public void EdittblUser(tblUser t)
{
    var q = db.tblUsers.SingleOrDefault(u => u.userName == t.userName );
    if(q==null)
       // throw exception

    q.Property1=t.Property1;
    q.Property2=t.Property2;
    // and so on
    dbconnect.submitchange();
}

where Property1, Property2 and so on are the properties of the user object. (For instance they would be the userId, the userName and so on).

The other way for editing your record would be using AutoMapper this way:

// I suppose that you have made a reference of AutoMapper assembly in your project.
// Having done this then you should include it to your source file using the using 
// statement 
using AutoMapper;

public void EdittblUser(tblUser t)
{
    Mapper.CreateMap<Model1, Model2>();
    var q = db.tblUsers.SingleOrDefault(u => u.userName == t.userName );
    q = Mapper.Map(Model1, Model2)(t); 
    dbconnect.submitchange();
}

Where Model1 and Model2 are the classes you want to create a mapping between them.

However, since both t and q are of the same type, I don't see why you should use this.
For further clarification about AutoMapper, please check this one link in codeproject:

[http://www.codeproject.com/Articles/639618/CRUD-Opearations-using-AutoMapper-in-an-MVC-Applic][1]

Upvotes: 2

Related Questions