我创建了一个新的C#ASP.NET动态数据网站,它为我的EDMX文件中的所有表实体提供了CRUD功能。
以下表格在文件中:
Customers
-----------
CustomerId
CustomerName
Documents
-----------
DocumentId
DocumentName
DocumentType
CustomerId
CustomerId是客户中的PK和文档中的FK。但是,当Dynamic Web App显示Documents中的所有行时,我想在GridView中显示以下列:
Documents
-----------
DocumentId
DocumentName
DocumentType
CustomerId
CustomerName
当用户看到所有文档列表时,它们也很重要,他们也可以看到哪个CustomerName与每个文档相关联。我不想从“文档”列表中编辑CustomerName。它只是为了帮助观看。如何在Dynamic Data Web App中查看Document GridView中的CustomerName?
我在VS2012使用了Code First From Database,这是从EF生成的实体:
namespace DocMappings
{
using System;
using System.Collections.Generic;
public partial class Customers
{
public Customers()
{
this.Documents = new HashSet<Documents >();
}
public int CustomerId { get; set; }
public string CustomerName { get; set; }
public virtual ICollection<Documents> Documents { get; set; }
}
}
文件实体是:
namespace DocMappings
{
using System;
using System.Collections.Generic;
public partial class Documents
{
public int DocumentId { get; set; }
public string DocumentName { get; set; }
public int DocumentType{ get; set; }
public int CustomerId { get; set; }
public virtual Customers Customers { get; set; }
}
}
答案 0 :(得分:1)
您可以为数据创建匿名类型:
var docs = context.Documents.SelectMany(d => new
{
d.DocumentId,
d.DocumentName,
d.DocumentType,
d.CustomerId,
d.Customer.CustomerName
});
或者,最好将其纳入具体类别:
public class CustomerDocument
{
public int DocumentId { get; set; }
public string DocumentName { get; set; }
public int DocumentType { get; set; }
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
稍微修改后获取数据:
List<CustomerDocument> docs = context.Documents.SelectMany(d => new CustomerDocument
{
d.DocumentId,
d.DocumentName,
d.DocumentType,
d.CustomerId,
d.Customer.CustomerName
});