Reputation: 110
I am currently starting with a new tool called Webmatrix for creating websites based on CSHTML. Now I have some trouble with starting and cant find any help on other pages: I create Database call
var db = Database.Open("MyDB");
var selectQueryString = "SELECT * FROM a JOIN b ON a.id = b.id";
a and b both have the field "name" but I cant find any way to separate. I already tried
@foreach(var row in db.Query(selectQueryString)){
<tr>
<td>@row.id</td>
<td>@row.a.name</td>
<td>@row.b.name</td>
</tr>
}
As I know it from MySQL in PHP. But that doesnt work. Can anybody help me with this (I guess) very simple question?
Upvotes: 0
Views: 476
Reputation: 21
Use entity framework, create separate class for data application layer, add Dbcontext, connection string put to web.config, and if you want looping two entities on the same time, you need something like this ->
var results = from ek in dbContext.tbl_EKTransfer
where //...(what you want)
select ek.ServisID
var results = from t in dbContext.tbl_Transfer
where // ...(what you want)
select t.ServisID;
results = results.Union(results2);
foreach (var result in results)
{
}
Or join second table.
Upvotes: 0
Reputation: 103388
Specify the columns you require rather than using a wildcard asterisk.
Aside from resolving your issue this is also much better for performance.
var selectQueryString = "SELECT a.id, a.name as AName, b.name as BName FROM a JOIN b ON a.id = b.id";
Then you can do:
@foreach(var row in db.Query(selectQueryString)){
<tr>
<td>@row.id</td>
<td>@row.AName</td>
<td>@row.BName</td>
</tr>
}
Upvotes: 1