Hassan Hosseini
Hassan Hosseini

Reputation: 420

how can i use ASP linq gridview to read different data name from two foreign keys in one table

I am using ASP and SQL Server.

Scenario: One of my tables in the database twice a foreign key is a specific table used.

The problem: When I want to display different data entered on these foreign keys by linq in ASP GridView, it displays the same data for different values.

Example: My first table that name "Table_1" with two data:

id2=Book, id4=Student

enter image description here

and i have second table with name "foreignKeys" too that has data like this:

enter image description here

Question: how can i use linq gridview to read different data name (Book and Student) from "foreignKeys"?

My Html code:

<asp:GridView ID="gvDeFrm" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="linq">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Book"></asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Student"></asp:BoundField>
                </Columns>
            </asp:GridView>

My behind code:

 protected void linq_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = (from f in db.foreignkey
                    select new
                    {
                        f.userId,
                        f.Table_1.Name
                    }
                  ).ToList();
    }

Thank you for your attention earlier.

Upvotes: 0

Views: 59

Answers (1)

Greg Low
Greg Low

Reputation: 1586

You just need to have two from clauses instead of one. Give each one a different alias, and make sure each has a where clause that specifies what's joined to what.

At present, you just have a single from clause for the foreign key.

Take a look at the solution for this previous question: c# Linq Join same table twice

It's similar enough that it should give you what you need.

Upvotes: 1

Related Questions