【发布时间】:2013-12-02 01:37:59
【问题描述】:
我有一个这样设计的模型:
public class SearchListing
{
public int property_id { get; set; }
public int office_property_id { get; set; }
public String office_property_description { get; set; }
public String property_name { get; set; }
public String property_street { get; set; }
public String property_city { get; set; }
public String property_state { get; set; }
public String property_zip { get; set; }
public Nullable<int> numBedrooms { get; set; }
public Nullable<int> numBathrooms { get; set; }
public bool numGarages { get; set; }
}
我打算使用一个 linq 语句来填充这个模型。该模型可以单独使用,也可以有一个搜索列表列表。我的 linq 语句必须能够检索符合条件的所有属性。
属性与 officeproperties 具有一对多的关系。因此,一个物业可能有多个关于该物业的办公室特定信息。我想随机选择其中一个办公室来提供他们有关该物业的信息。我有以下 linq:
propertyIDSearch = (from p in db.Property
join bp in db.OfficeProperty on p.property_id equals bp.property_id
join b in db.Office on bp.office_id equals b.office_id
where b.status == 1 && bp.active_listing == true
orderby Guid.NewGuid()
select new SearchListing
{
property_id = p.property_id,
office_property_id = bp.office_property_id,
office_property_description = bp.description,
property_name = p.prop_name,
property_street = p.street,
property_city = p.city,
property_state = p.state,
property_zip = p.zip,
numBedrooms = p.bed_rooms,
numBathrooms = p.baths,
numGarages = p.garage
})
.Take(1)
.ToList<SearchListing>();
这可行,但只给了我一个属性。我正在考虑执行以下操作,但后来我无法在语句中使用 p.property_id:
propertyIDSearch = (from p in db.Property
select new SearchListing
{
property_id = p.property_id,
office_property_id = (from bp in OfficeProperty
join b in Office on b.office_id equals bp.office_id
where p.property_id equals bp.property_id
select bp.office_property_id)
office_property_description = bp.description,
property_name = p.prop_name,
property_street = p.street,
property_city = p.city,
property_state = p.state,
property_zip = p.zip,
numBedrooms = p.bed_rooms,
numBathrooms = p.baths,
numGarages = p.garage
})
.ToList<SearchListing>();
该代码不起作用,只是为了显示思考过程而编写的。我需要使用该特定属性的办公室的一个随机实例填写办公室属性描述和办公室属性 ID。如果我像刚才那样在 select 语句中运行 linq,我就不能使用该实例来获取描述。如果我尝试在加入中添加,我将无法使用 p.property id:
propertyIDSearch = (from p in db.Property
join bp in (from bp in OfficeProperty
join b in Office on b.office_id equals bp.office_id
where p.property_id equals bp.property_id
select bp).ToList()[new Random().Next(list.count)] on p.property_id equals bp.property_id
select new SearchListing
{
property_id = p.property_id,
office_property_id = bp.office_property_id
office_property_description = bp.description,
property_name = p.prop_name,
property_street = p.street,
property_city = p.city,
property_state = p.state,
property_zip = p.zip,
numBedrooms = p.bed_rooms,
numBathrooms = p.baths,
numGarages = p.garage
})
.ToList<SearchListing>();
这也不起作用。
编辑:
我根据提供的建议进行了尝试,这似乎有效。这种方法有什么不好的地方吗?
propertyIDSearch = (from p in db.Property
join bp in db.OfficeProperty on p.property_id equals bp.property_id
where bp.Officeproperty_id == (from bp2 in db.OfficeProperty
join b in db.Office on bp2.Office_id equals b.Office_id
where b.status == 1 && bp2.active_listing == true && bp2.property_id == p.property_id
orderby Guid.NewGuid()
select bp2.Office_property_id).Take(1).FirstOrDefault()
//Build the where clauses based off of the search criteria
where p.property_id == (searchCriteria.propertyID > 0 ? searchCriteria.propertyID : p.property_id)
where p.geography_id == (searchCriteria.citySelected > 0 ? searchCriteria.citySelected : p.geography_id)
select new SearchListing
{
property_id = p.property_id,
Office_property_id = bp.Office_property_id,
Office_property_description = bp.description,
property_name = p.prop_name,
property_street = p.street,
property_city = p.city,
property_state = p.state,
property_zip = p.zip,
numBedrooms = p.bed_rooms,
numBathrooms = p.baths,
numGarages = p.garage
}).ToList<SearchListing>();
【问题讨论】:
-
我修改了我的代码,你介意看看吗?
标签: c# .net asp.net-mvc linq entity-framework