Reputation: 337
I have generated the classes from the database using scaffold.
public partial class Class1
{
public int ID { get; set; }
public int Class2ID { get; set; }
public string Name { get; set; }
public DateTime Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public virtual Class2 Class2 { get; set; }
}
public partial class Class2
{
public Class2()
{
Class1 = new HashSet<Class1>();
}
public int ID { get; set; }
public string Allotment { get; set; }
public string Manual { get; set; }
public virtual ICollection<Class1>Class1 {get; set;}
}
So there is a requirement to join these two tables in entity and get the data. And I am getting the result.
Here is my code:
var _class1Repo = UnitWork.GetGenericRepositoryFor<Class1>().AsNoTracking();
var _class2Repo = UnitWork.GetGenericRepositoryFor<Class2>().AsNoTracking();
var query = from _cls1 in _class1 in _class1Repo
join _cls2 in _class2Repo on _cls1.Class2ID = _cls2.ID
where _cls2.ID = 2
select new Class1() {
Name =_cls1 .Name,
Address =_cls1.Address,
City =_cls1 .City,
Country =_cls1.Country
// I want just only one property value inside this from class2
};
I need to include Class2 one property. How can I do that? Is there a way to achieve this?
Upvotes: 1
Views: 119
Reputation: 20930
Define your Class1.cs
like this:
public class Class1
{
public int ID { get; set; }
public int Class2ID { get; set; }
public string Name { get; set; }
public DateTime Address { get; set; }
public string City { get; set; }
public string Country { get; set; }
public virtual Class2 Class2 { get; set; }
public Class1() {}
// Define this constructor
}
A constructor
is a special method of the class which gets automatically invoked whenever an instance of the class is created.
So whenever you created instance you can easy to access the property.
Upvotes: 2