【发布时间】:2011-11-14 03:18:54
【问题描述】:
如何从具有一对多关系的 LINQ 查询返回方法中的集合?
例如,我有以下代码,其中我可以有多个项目到 TimeTracking 对象。我为返回类型 (IEnumerable) 定义的类型是否有效?它在我的 EF 模型中设置为这种特定的关系。
public IEnumerable<TimeTracking> GetTimeTrackings()
{
YeagerTechEntities DbContext = new YeagerTechEntities();
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = false;
var timeTrackings = (from timeTrackingProjects in DbContext.TimeTrackings.Include("TimeTrackings.Projects")
select timeTrackingProjects).Where(p => p.TimeTrackingID > 0);
CloseConnection(DbContext);
return timeTrackings;
}
如果是这样,当我在 MVC 3 视图中显示它并且我的视图包含 IEnumerable<YeagerTech.YeagerTechWcfService.TimeTracking> 模型时,模型变量中是否包含 TimeTracking 和 Project 对象的记录?我不认为它会。我的 TimeTracking 对象设置如下,除非我需要用它继承 Project 类(然后将具有 Project 属性):
public partial class TimeTracking
{
[DataMember]
public int TimeTrackingID { get; set; }
[DataMember]
public short ProjectID { get; set; }
[DataMember]
public byte[] Attachment { get; set; }
[Required]
[DataType(DataType.Date)]
[DataMember]
public System.DateTime StartDate { get; set; }
[Required]
[DataType(DataType.Date)]
[DataMember]
public System.DateTime EndDate { get; set; }
[DataMember]
public string Notes { get; set; }
[DataMember]
public System.DateTime CreatedDate { get; set; }
[DataMember]
public Nullable<System.DateTime> UpdatedDate { get; set; }
[DataMember]
public virtual Project Project { get; set; }
}
我还希望我的视图显示与 TimeTracking 相关联的项目文本,而不是项目值。我该怎么做?有人可以帮忙吗?
我在 WCF 客户端上调用方法时收到以下消息。
'如果禁用引用跟踪,则无法序列化'
得到消息后,我修改了我的 DataContracts 以包含引用 ([DataContract(IsReference = true)])。
namespace YeagerTechModel
{
[Serializable]
[DataContract(IsReference = true)]
public partial class Customer
{
public Customer()
{
this.Projects = new HashSet<Project>();
}
[DataMember]
public short CustomerID { get; set; }
[Required]
[StringLength(50)]
[DataType(DataType.EmailAddress)]
[DataMember]
public string Email { get; set; }
我正在执行以下服务器端代码,以成功从我的数据库中以父/子关系获取数据。 Include 方法显式调用获取特定客户的相关项目数据。我必须这样做,因为如果你想通过网络获取你的父/子数据,你必须关闭 LazyLoading。
如果我查看 WCF 消息日志,我可以看到 Customer 对象中的实际数据,并且它在 Customer 对象中包含 Project 对象。
但是,在调用之后,我实际上检查了“客户”变量的内容,我没有看到对任何项目数据的任何引用。
public IEnumerable<Customer> GetCustomers()
{
YeagerTechEntities DbContext = new YeagerTechEntities();
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = false;
IQueryable<Customer> customer = DbContext.Customers.Include("Projects").Where(p => p.CustomerID > 0);
CloseConnection(DbContext);
return customer;
}
我现在想做的是引用从调用中返回的项目数据。但是,在输入“客户”后,我没有得到任何客户对象智能感知。这一切都与 IQueryable 对象有关。
我将它作为以下类型传递回我的 MVC 控制器:
IEnumerable<YeagerTechWcfService.Customer> customerList = db.GetCustomers();
并作为以下模型进入我的视图:
IEnumerable<YeagerTech.YeagerTechWcfService.Customer>
现在,最大的问题是“我如何引用返回到我的视图中的项目数据?
以下是我的视图代码,但“item.Project”没有智能感知。请注意,“Email”是我的 Customer 对象中的一个属性。
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
【问题讨论】:
标签: linq asp.net-mvc-3 linq-to-entities entity-framework-4.1