简体   繁体   English

使用Linq从两个表中获取数据

[英]Fetch data From Two tables using Linq

all 1st of all I want to say I am totally new to Entity Framework but I want to learn it so My question is I have three tables. 首先,我想说我对Entity Framework完全不熟悉,但我想学习它,所以我的问题是我有三张桌子。 1st for storing author details. 第一个存储作者详细信息。
Structure of the table is 表的结构是

在此输入图像描述

2nd table for storing all the articles 第二个表用于存储所有文章
Structure of the table is 表的结构是

在此输入图像描述

and Table 3 for Storing comments 和表3用于存储评论
Structure of the table is 表的结构是

在此输入图像描述

and as My client want I have no foreign key in Database but they have relations and I am handling them from code (Please don't ask why because I also don't know why my client don't want foreign Key) :) 并且作为我的客户想要我在数据库中没有外键但他们有关系,我从代码处理它们(请不要问为什么因为我也不知道为什么我的客户端不想要外键):)

What I am doing Now I have an Admin Panel where there is a Grid View Where I am binding the Author Table and on Clicking of Any Row it will open another page 我正在做什么现在我有一个管理面板,其中有一个网格视图我在哪里绑定作者表和单击任何行它将打开另一个页面

Page source Code Example: 页面源代码示例:

<div>
 <asp:ListView ID="articles" runat="server">
<ItemTemplate>
Label1//Here Goes the Article Id
Label2//Here Goes the Article Names
<div>
//Another Listview For Comments
 <asp:ListView ID="comments" runat="server">
<ItemTemplate>
Here Goes All the Comments
</ItemTemplate>
</asp:ListView>
</div>
</ItemTemplate>
</asp:ListView>

So What I am doing First I am binding the Article List and after binding I am doing a Foreach Loop Inside the article List to find all the article id and the Comment List view Note: As for the code You can see the comment list will be repeated for each article Once I find the Comment ID the I fetch the comments for that id and bind it to the Comment List. 所以我在做什么首先我绑定文章列表并在绑定后我正在做一个Foreach循环在文章列表中查找所有文章ID和评论列表视图注意:至于代码你可以看到评论列表将是为每篇文章重复一旦找到了注释ID,我就会获取该ID的注释并将其绑定到注释列表。

So here my problem is I am connecting to the DB for two times but I want to do this in a Single connection is there any way by which I can fetch all the rows of comment table for the article id while fetching the Articles. 所以这里我的问题是我连接到数据库两次但我想在单连接中执行此操作是否有任何方法可以在获取文章时获取文章ID的所有注释表行。

My Article Fetching code is 我的文章提取代码是

Context.Artcles.Select(x=>x.x.RefAuth==AuthId).ToList();

I will get the AuthID from GridView Row Click. 我将从GridView Row Click中获取AuthID。

I know it is not a huge thing but I am totally new in this. 我知道这不是一件大事,但我完全是新手。

Question:2 问题2

Thanks to Ehsan Sajjad for giving me the perfect solution So there is another Question. 感谢Ehsan Sajjad为我提供了完美的解决方案所以还有另一个问题。 In my first Question I was getting data from two tables so The suggestion was to use Join in Linq but in my Second Question I have one table and the Table format is like this. 在我的第一个问题中,我从两个表中获取数据,因此建议使用Linq中的Join,但在我的第二个问题中,我有一个表,Table格式是这样的。
在此输入图像描述
If the type is 1 then Received else if the type is 2 then replied. 如果类型为1,则在类型为2时接收else,然后回复。
Refmailid is when I will reply to a mail for ex if "mailid" is 1 then for that reply "refmailid" will be 1 by which I can fetch all the replies of one mail So what I want, just like articles and comments here also I am fetching all the received mails from a user the from Column is a Unique Userid Refmailid是我什么时候回复邮件,如果“mailid”为1那么对于那个回复“refmailid”将是1,我可以通过它获取一封邮件的所有回复所以我想要的,就像这里的文章和评论一样我从用户获取所有收到的邮件,而Column是一个唯一的用户ID

so when I will fetch 所以当我要取

Context.SupportRequests.Select(x=>x.from=="theuniqueuserid" && type==1).ToList();

I will get all the received mails from that use and just like the previous div 我将收到所有收到的邮件,就像之前的div一样

<div>
     <asp:ListView ID="receivedmails" runat="server">
    <ItemTemplate>
    Label1//Here Goes subject
    Label2//Here Goes the content
    <div>
    //Another Listview For Replies
     <asp:ListView ID="replies" runat="server">
    <ItemTemplate>Here Goes All the replies will be more then one so it will be reapeated
with 
//Subject
//Content
    </ItemTemplate>
    </asp:ListView>
    </div>
    </ItemTemplate>
    </asp:ListView>
</div>

So What I am doing? 那我在做什么? First I am binding the Received List and after binding I am doing a Foreach Loop Inside the Received List to find all the "mailid" and the replied List view Note: As for the code You can see the replied list will be repeated for each received mail Once I find the "mailid" then I will fetch the replies for that id and bind it to the reply List. 首先我绑定收到的列表,绑定后我在收到的列表中做一个Foreach循环,找到所有的“mailid”和回复的列表视图注意:代码你可以看到每个收到的回复列表都会重复mail一旦找到“mailid”,我将获取该id的回复并将其绑定到回复列表。 Code for that 代码

 Context.SupportRequests.Select(x=>x.refmailid=="mailid").ToList();

You need join 你需要加入

var result = (from a in Context.Articles
             join c in Context.Comments on a.ArticleId equals c.ArticleId
             select new{ Article = a, Comment = c});

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

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