1

I have a viewmodel that contains two lists. One is from a property table and one is the office information for that property. In SQL if I wanted both of the tables I could do this:

Select o.*, p.*, op.* From Property p
Inner Join OfficeProperty op On p.property_id = op.property_id
Inner Join Broker b On b.broker_id = op.broker_id
Inner Join BrokerOffice bo On bo.broker_id = b.broker_id
Where p.property_id = 5555
And b.active = 1 And bo.office_type = 'Main' And op.active_listing = 1

This would give me everything from the office related to that property and all the property info. If I have the following viewmodel:

public class CompletePropertyInfo
{     
  public Property property { get; set; }
  public OfficeProperty officeProperty { get; set; }
  public Office { get; set; }
}

public class myViewModel
{     
  public List<CompletePropertyInfo> propertyInfoList { get; set; }
}

Maybe with what I am asking in Edit 2, maybe I should have this for CompletePropertyInfo:

public class CompletePropertyInfo
{     
  public Property property { get; set; }
  public List<OfficeProperty> officeProperty { get; set; }
  public List<Office> { get; set; }
}

How could I fill the property list with property.* and the office list with office.* etc?

I feel like I shouldn't have to run three statements of the same criteria with different selections to fill them. Maybe this is the only way?

EDIT: I expanded upon the example code to better show what my actual code is like. I can't copy and paste the exact code for security reasons.

EDIT 2: A note on the real world application on this, I'm trying to make a property search for our internal system. I either want a bunch of properties based on a criteria or just one if a property id is supplied.

I realized I mixed the two classes I was looking at together when I saw everyone's comments about why there is a list of the original property. This should be the correct code now....I'm sorry about my mistakes, I'll have to triple check what I write next time. I have edited the SQL to show almost exactly what I am doing inner joining four tables but only wanting to fill the models of three of those tables. I don't need the broker table to be stored, just need it to filter out some records for the broker office. The ViewModel is just a list of all the CompletePropertyInfos that I get. CompletePropertyInfo is what I am trying to fill from one statement instead of breaking the above sql into three separate statements. The list would be either size one for a specific property search or multiple for more properties.

I eventually will have to do a LINQ statement the creates a list of CompletePropertyInfos from which is why I was thinking of the lists originally as the search LINQ will have to bring back a list of properties and there respective offices. The SQL would be the same just without the p.property_id = 5555 so it would return all the properties in the system and I would want that in a List< CompletePropertyInfo >.

TLDR: I'm wondering if I can fill all my results into a list of CompletePropertyInfo via one linq statement. The list would be either size one for a specific property search or multiple for more properties.

public class CompletePropertyInformation
{
    public RR_Property property { get; set; }
    public IQueryable<OfficeProperty> officePropertyList { get; set; }
    public IQueryable<Office> officeList { get; set; }
}

List<CompletePropertyInformation> propertyIDSearch = (from p in db.Property
                                                                      where p.property_id == searchCriteria.propertyID
                                                                      select new CompletePropertyInformation
                                                                      {
                                                                          property = p,
                                                                          officePropertyList = (from bp in db.officeProperty                                                                                                
                                                                                            where bp.active_listing == true && bp.property_id == p.property_id
                                                                                            select bp),
                                                                          OfficeList = (from bo in db.Office                                                                                              
                                                                                          join bp in db.officeProperty on b.office_id equals bp.office_id
                                                                                          where bp.active_listing == true && bp.property_id == p.property_id && bo.office_type == "P"
                                                                                          select bo)
                                                                                }).ToList<CompletePropertyInformation>();

EDIT 3: I got something working for me and I am posting the code above. With this I have two questions

  1. Is this the right/best way to handle this?
  2. How can I reword the question so I can let others better know what I was trying to do, fill a model with a few lists.
10
  • Are you using Entity Framework? Nov 27, 2013 at 4:12
  • Yes. I am using Entity Framework to turn my database tables into model objects. Database first approach. Nov 27, 2013 at 4:18
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Nov 27, 2013 at 4:28
  • your context object is having all the offices and if they are related in your db (you said you did a db first), you can then access o.Properties for each office object to get your property list.
    – Kubi
    Nov 27, 2013 at 4:31
  • 1
    Your view model does not feel right. (why) If you are pulling a property by id (I presume that property_id is a primary key in Property table) the it will result at most in one property hence you view model should be 'public class myViewModel { public Property property { get; set; } public List<Office> { get; set; } }'. Once you get property simply run 'Select o.* From Office o Where o._id = 5555' to get all your office and assign them to Offices property of you view.
    – Max
    Nov 27, 2013 at 4:35

3 Answers 3

1

Considering that it appears your Property objects have many Office objects, you should adjust your view model to have one property with a list of offices. In which case, you can easily do the following LINQ statement:

var myModel = db.Properties.Where(property => property_id = 5555)
                           .Select(p => new myViewModel { Property = p, Offices = p.Offices });

EDIT: The difference in the structure from your edit does not affect the LINQ query as much as you would think. Here's is what you would do in order to get both of those lists together:

var myModel = db.Properties.Where(property => property_id = 5555)
                           .Select(p => new myViewModel { Property = p, 
                                                          OfficeProperty = p.OfficeProperties,
                                                          Offices = p.OfficeProperties.Select(op => op.Office) });
3
  • The structure is a bit different. Hopefully I better explained myself in the edit to the code I made above. Nov 27, 2013 at 4:58
  • I edited my question again and left a comment on the main question. It should now completely explain what I'm trying to do. For your edit 2, how does p.OfficeProperies get filled? I'm not familiar with that syntax, it makes it seem like OfficeProperties is a database column of Property which it isn't. They are separate tables joined on property_id. Nov 27, 2013 at 14:12
  • I think I got something working and I'll make it an edit 3 tomorrow to show you what I am thinking and we can check if it is correct. Nov 28, 2013 at 5:08
0

Consider that you have two tables. one is parent table 'TableA' and child table 'TableB'. And TableB contains one foreign key of TableA.

If you are using LINQ, you do not have to perform join. It will retrieve data from parent as well as child table.

When you write the query

var data  = db.TableA.Where(a => a.id == 1).FirstOrDefault();

You can access the data from TableB as well like:

var tableBId = data.TableB.Id;
0
0

your context object is having all Office objects and if they are related in your db (you said you did a db first), you can then access o.Properties for each office object to get your property list and populate your viewmodel with your repository method.

    T YourRepositoryMethod(){
    using(YourEntities dc)
    {
    var offices = dc.Offices;
    var properties = dc.Properties;
var office1 = dc.Offices.single(o=>o.id == office1id);
    var office1Properties = dc.Properties.Where(p=>p.id == office1._id);
    }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.