ASP.NET LINQ Join no funcionará
Frecuentes
Visto 407 veces
1
I'm making a new application in ASP. I want an output with all joined data. I work in 3 layers.
So here is the join code
Public Function selectAllOpenBugs() As List(Of tbl_bug)
Dim result = (From b In dc.tbl_bugs
Join p In dc.tbl_priorities On b.BugPriority Equals p.priorityId
Join u In dc.tbl_users On b.BugOwner Equals u.userId
Join u1 In dc.tbl_users On b.BugAssigned Equals u1.userId
Where b.BugStatus = 1
Select b).ToList
Return result
End Function
Here is the code to get the results
Public Function selectOpen() As List(Of tbl_bug)
Return DALBugs.selectAllOpenBugs()
End Function
And here is the code to fill the repeater
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
rptOpenBugs.DataSource = BBLBugs.selectOpen()
rptOpenBugs.DataBind()
End Sub
And this is what the repeater shows
BugId BugTitle BugPriority BugStatus BugOwner BugAssigned BugProject BugPriority
1 TEST 1 1 1 2 1 tbl_priority
Why doesn't the repeater show the joined values?
Gracias de antemano!
SOLUTION
You guys obviosly didn't understand my question. I work with entities i forgot to mension that in the question.
The problem wasn't to select the right values. I just needed to get the values in the repeater. Like this
<asp:Content ID="Content1" runat="server" contentplaceholderid="MainContent">
<table>
<asp:Repeater ID="rptOpenBugs" runat="server">
<ItemTemplate>
<tr>
<td><asp:Label ID="lblID" runat="server" Text='<%# Eval("BugId")%>'></asp:Label></td>
<td><asp:Label ID="lblTitel" runat="server" Text='<%# Eval("BugTitle")%>'></asp:Label></td>
<td><asp:Label ID="lblPrioriteit" runat="server" Text='<%# Eval("tbl_priority.priorityName")%>'></asp:Label> </td>
[....]
</td>
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
1 Respuestas
4
Umm.. because you're only selecting b? Is this a trick question?
Linq only selects what you tell it to select, and you told it select b. If you want other values, then you need to select those as well. I don't know what the VB syntax for that would be, but in c# it would be something like this:
var result = (from b In dc.tbl_bugs
join p In dc.tbl_priorities on b.BugPriority equals p.priorityId
join u In dc.tbl_users on b.BugOwner equals u.userId
join u1 In dc.tbl_users on b.BugAssigned equals u1.userId
where b.BugStatus == 1
select new {Bugs = b, Prioritiy = p.Foo, user = u1.Bar }).ToList();
EDIT:
Based on your answer, you're going about things the hard way. Based on your answer, you have navigational properties, and those properties have implicit joins. You could write your query like this:
var result = (from b in dc.tbl_bugs where BugStatus == 1).ToList()
Accessing the navigational properties provides an implicit table join, and you can simply access tbl_priority or BugAssigned or whatever using the navigational property. You only need to use the join syntax when there isn't a navigational property.
Respondido 24 ago 12, 06:08
Thanks for the answer. I remember in my previous program i did the same and it worked there(maybe i just remember it wrong). Anyway, can you maybe help me out how to do that please? - Nick
Hi, Thanks for the code example. But there is still one problem. Now i can't do this anymore: "Public Function selectAllOpenBugs() As List(Of tbl_bug)" and that is the whole point of LINQ, no? - Nick
@schoen - No, you can't return tbl_bug because tbl_bug does not contain the data you want, unless tbl_bug has a navigational property to the tbl_priorities table. And if it has that navigational property, you don't need the join because joins are inferred from the navigation. - Erik Funkenbusch
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net .net sql linq or haz tu propia pregunta.
My mistake, it wasn't clear from your answer that it was a solution. If it is, then go ahead and post it as an answer, but make it clear that this is the case. - Erik Funkenbusch