简体   繁体   English

实体框架加入三个表并使用组Concat

[英]Entity Framework Joining Three Tables and Using Group Concat

I have three models like followings, 我有三个模型,如下面,

    public class Team
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Document
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string Application { get; set; }
        public ICollection<DocumentResponsible> DocumentResponsibles { get; set; }
        public string Pcda { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }
    public class DocumentResponsible
    {
        public int Id { get; set; }
        public int DocumentId { get; set; }
        public int TeamId { get; set; }
    }

I want to write an entity framework expression to join three table and select all fields of document table and team names in one row per documents. 我想编写一个实体框架表达式来连接三个表,并在每个文档的一行中选择文档表和团队名称的所有字段。 So basicly I want to join three table use group_concat for team names. 所以基本上我想加入三个表使用group_concat作为团队名称。 Then I want to bind it to a gridview in web form. 然后我想将它绑定到web表单中的gridview。

What I have tried, 我试过的,

(from dc in DBContext.Document
join dr in DBContext.DocumentResponsible on dc.Id equals dr.DocumentId
join t in DBContext.Team on dr.TeamId equals t.Id
select new
{
    Name = dc.Name,
    Type = dc.Type,
    Application = dc.Application,
    Pcda = dc.Pcda,
}).ToList();

and I have just tried it, 而我刚尝试过,

var data = DBContext.Dcoument.Include("DocumentResponsibles").Tolist();

It's hard to help without your DbContext and the Entity Mappings, but I'll go out on a limb saying you might just want to mark Document.DocumentResponsibles as virtual. 没有你的DbContext和实体映射很难提供帮助,但是我会说你可能只想将Document.DocumentResponsibles标记为虚拟。

Also, in DocumentResponsible , maybe you'd want to add a property for Document and one for Team (both marked as virtual too) this way you don't have to do the join keys all the time you want to work with your data, EF would do it for you once properly configured. 此外,在DocumentResponsible ,您可能想要为Document添加一个属性,为Team添加一个属性(也标记为虚拟),这样您就不必一直使用连接键来处理数据,一旦正确配置,EF会为您完成。

If it doesn't work, can you add the following information to your question: First, the context class and the mappings you have. 如果它不起作用,您可以在您的问题中添加以下信息:首先,上下文类和您拥有的映射。 Second, if you do var firstDocument = yoneylemDB.Document.First() , how does firstDocument looks like? 其次,如果你做var firstDocument = yoneylemDB.Document.First()firstDocument是怎么样的? Does it have all it's fields and properties filled out? 它是否填写了所有的字段和属性? Is there anything weird? 有什么奇怪的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM