0

I have an Entity model with entities of Person and LogPerson. they are identical except that LogPerson has 2 additional fields (LogPersonID, CreateDate). How can I cast a LogPerson into a Person so that the VB.NET code that follows my Linq code doesn't have to try to work with both possible types?

For example:

dim p as person
If useLog then
   p = From a In LogPerson Where ID = x
Else
   p = From a In Person Where ID = x
End If

textbox1.text = p.firstname
ronalchn
  • 12,225
  • 10
  • 51
  • 61
gjones
  • 37
  • 1
  • 9
  • 1
    What you are trying to do is "auto-map". [This question](http://stackoverflow.com/questions/3074617/automapping-custom-generic-types-how) might be of some help. – Steve Konves Sep 13 '12 at 20:33
  • No, just work with a particular table in a particular circumstance (view from log, view active record) – gjones Sep 13 '12 at 20:46

2 Answers2

0

There aren't any ways out-of-the box. The easiest way without third party tools/libraries is to use the Select method and map the properties manually, like so:

IEnumerable<Person> people = ...;

IEnumerable<LogPerson> logPeople = people.Select(p => new LogPerson { 
    Name = p.Name,
    Age = p.Age,
    ...
});

Of course, this is tedious, so if you have a large number of fields or a number of places you have to perform this kind of operation, you might want to look into an auto mapping library, such as AutoMapper.

casperOne
  • 73,706
  • 19
  • 184
  • 253
0
I am WRITING THE SOLUTION IN C#....

person p;
if(uselog)
{
var per = from pobj in LogPerson
          where pobj.ID= x
          select new person{
          firstname = pobj.firstname,
          lastname = pobj.lastname,
          };
}else
{
var per = from pobj in Person
    where pobj.ID= x
    select new person{
    firstname = pobj.firstname,
    lastname = pobj.lastname,
    };
}
p = per.first();
}


textbox1.text = p.firstname
Pushpendra
  • 548
  • 3
  • 10