Laziale
Laziale

Reputation: 8225

linq query which will return single values and a list in one call

I have this linq call:

PropertyViewModel propertyModel = null;
    propertyModel = (from property in db.LoanProperties
                    join tenant in db.Tenants
                    on property.PropertyId equals tenant.PropertyId
                    where property.LoanApplicationId == newApplication.LoanId
                    select new PropertyViewModel(
                        propertyModel.AddressLine1 = property.AddressLine1,
                        propertyModel.AddressLine2 = property.AddressLine2,
                        propertyModel.TotalRentPerAnnum = property.TotalRentPerAnnum,
                        propertyModel.Tenants = db.Tenants.Where(s => s.PropertyId == property.PropertyId).ToList()
                        ));

My view model:

public class PropertyViewModel
    {
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public decimal? TotalRentPerAnnum { get; set; }
        public List<TenantViewModel> Tenants { get; set; }
    }

I want to cast the tenants under the property from the linq query in my TenantViewModel.

How can I achieve that?

I'm referring to the last line for propertyModel.Tenants.

Upvotes: 0

Views: 531

Answers (2)

Damien
Damien

Reputation: 53

it's my first answer !! hope it's help :

The objects Tenants and LoanProperties must have a navigation property with the Foreign key PropertyId. So, a LoanProperties object must have a list of Tenants.

I prefer the lambda expressions as you used on the last line (clean/clear code). Try this :

var propertyModel = db.LoanProperties
        .Where(p => p.LoanApplicationId == newApplication.LoanId)
        .Select(p => new PropertyViewModel(){
                AddressLine1 = p.AddressLine1,
                AddressLine2 = p.AddressLine2,
                TotalRentPerAnnum = p.TotalRentPerAnnum,
                Tenants = p.Tenants.Select(t=> new TenantViewModel(){TenantType = t.TenantType , //other properties...})
                })
                //you don't have to query again, the tenants are already in the LoanProperty objects
                //you just have to transform it on ViewModel with a Select
        .FirstOrDefault();

Also, in your constructor method of PropertyViewModel, you don't have to put propertyModel.--- = ----. it's useless.

Upvotes: 2

woodykiddy
woodykiddy

Reputation: 6455

I hope I understood your question correctly. I guess you were looking for mapping your Tenant database object with your TenantViewModel?

ropertyViewModel propertyModel = null;
    propertyModel = (from property in db.LoanProperties
                    join tenant in db.Tenants
                    on property.PropertyId equals tenant.PropertyId
                    where property.LoanApplicationId == newApplication.LoanId
                    select new PropertyViewModel(
                        propertyModel.AddressLine1 = property.AddressLine1,
                        propertyModel.AddressLine2 = property.AddressLine2,
                        propertyModel.TotalRentPerAnnum = property.TotalRentPerAnnum,
                        propertyModel.Tenants = db.Tenants.Where(s => s.PropertyId == property.PropertyId).Select(t => new TenantViewModel {//map your properties}).ToList()
                        ));

Upvotes: 1

Related Questions