linq to sql. two relative tables - asp.net

I have two tables:
tab1 tab2
ID | Name | Sername | PostID ID | PostDecription
The question: how can I show in tab1 in cell PostID cell from tab2 PostDecription if PostDecription could has value NULL?
(from p in tab1 join s in tab2 on p.PostID equals
s.ID select new
{
ID = p.ID,
Name= p.Name,
Sername = p.Sername,
PostID = s.PostDecription,
})
Using this code I can get only cells that have the same value in two tables. What about case when PostDecription could has value "NULL"???

You need a left join
from p in tab1
join s in tab2 on p.PostID equals s.ID into tab2s
from s in tab2s.DefaultIfEmpty()
select new
{
ID = p.ID,
Name= p.Name,
Sername = p.Sername,
PostID = s.PostDecription,
}

Related

SQLite Foreign Key Relationship with Godot

This is the only way that I found to get the information from a foreign_key relationship. Is there a better way?
db.query("SELECT owner FROM dog_names;")
print(db.query_result)
print(db.query_result[0])
var o = db.query_result[0]["owner"]
print(o)
var o_str = "SELECT name FROM owners WHERE id = {int};"
var o_form = o_str.format({"int": o})
db.query(o_form)
print(db.query_result)
var query_str = "SELECT name FROM owners JOIN dogs ON owners.id = dogs.id;"
db.query(query_str)
print(db.query_result)
The below guide is from https://www.techonthenet.com/sqlite/joins.php
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Combine multiple datatables into single result in ASP.Net Core

I have four tables that are passed to the page in the context. Each of the 4 tables are connected by an Id column. I would like to display a page with a table that combines columns from each of the 4 tables. I would also like to sort or search data and display the results based on 1 or all tables (for example, if table 2 contains the column "Name", then the search results should show only rows that match that name)
public IList<AllTables> AllTableData {get;set;}
public async Task OnGetAsync(
string sortOrder,
string queryFilter,
string searchString)
{
//setting sort and search Params
//......
IQueryable<Table1> table1= _context.Table1.Select(f => f);
IQueryable<Table2> table2= _context.Table2.Select(f => f);
IQueryable<Table3> table3= _context.Table3.Select(f => f);
IQueryable<Table4> table4= _context.Table4.Select(f => f);
if (!String.IsNullOrEmpty(searchString))
{
// e.g. table1= table1.Where(s => s.Id.ToString() == searchString);
}
//Example for sorting
switch (sortOrder)
{
case "Id":
table1= table1.OrderBy(s => s.Id);
break;
case "Name":
table2= table2.OrderBy(s => s.Id);
break;
}
//How do I combine all four tables
AllTableData = await (Join Tables 1-4).AsNoTracking().ToListAsync();
}
I have tried to create a new class such as follows
public Class AllTables
{
public Table1 Table1{get;set;}
public Table2 Table2{get;set;}
public Table3 Table3{get;set;}
public Table4 Table4{get;set;}
}
And then combining the data like this
AllTableData= await table1
.Select(f=>new AllTables(
f,
table2.Where(g => g.Id== f.Id).FirstOrDefault(),
table3.Where(h => h.Id== f.Id).FirstOrDefault(),
table4.Where(i => i.Id== f.Id).FirstOrDefault(),
)).AsNoTracking().ToListAsync();
This gave me full table data, but when I tried to sort based on table2.Name, the sorting did not work.
You can do that in the following way:
IQueryable<Table1> table1 = _context.Table1;
IQueryable<Table2> table2 = _context.Table2;
IQueryable<Table3> table3 = _context.Table3;
IQueryable<Table4> table4 = _context.Table4;
var query =
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id into g
from t2 in g.DefaultIfEmpty()
join t3 in table3 on t1.Id equals t3.Id into g
from t3 in g.DefaultIfEmpty()
join t4 in table4 on t1.Id equals t4.Id into g
from t4 in g.DefaultIfEmpty()
select new AllTables
{
Table1 = t1,
Table2 = t2,
Table3 = t3,
Table4 = t4
};
if (!String.IsNullOrEmpty(searchString))
{
query = query.Where(s => s.Table1.Id.ToString() == searchString);
}
//Example for sorting
switch (sortOrder)
{
case "Id":
query = query.OrderBy(s => s.Table1.Id);
break;
case "Name":
query = query.OrderBy(s => s.Table2.Name);
break;
}
var AllTableData = await query.AsNoTracking().ToListAsync();

Android Sqlite Multiple Joins with Alias does not return anything

I have this query:
Cursor res = db.rawQuery("SELECT t1.Id, t2.EmpId FROM table1 t1
LEFT JOIN table2 t2 ON t1.t2Id = t2.Id
LEFT JOIN table3 t3 ON t1.t3Id = t3.Id
WHERE t1.Id = 90909", null);
Now when I go
if(res.moveToFirst()){
do{
MyObject obj = new MyObject();
obj.Id = res.getInt(res.getColumnIndex("t1.Id"));
MyObjectAsProperty objProp = new MyObjectAsProperty();
objProp.EmpId = res.getInt(res.getColumnIndex("t2.EmpId"));
}while(res.moveToNext());
}
The objProp.EmpId returns nothing (or 0). I looked at the cursor columns and it's Fields basically did not include the table alias names. How do I go about getting the values of those aliased fields?
Thanks!
EDIT
Apologies. I forgot to add that if for example it's referencing the same other table multiple times with the same field.
Here's updated query
Cursor res = db.rawQuery("SELECT t1.Id, t2.EmpId, t2b.EmpId FROM table1 t1
LEFT JOIN table2 t2 ON t1.t2Id = t2.Id
LEFT JOIN table2 t2b ON t1.t2bId = t2b.Id
WHERE t1.Id = 90909", null);
if(res.moveToFirst()){
do{
MyObject obj = new MyObject();
obj.Id = res.getInt(res.getColumnIndex("t1.Id"));
MyObjectAsProperty objProp = new MyObjectAsProperty();
objProp.EmpId = res.getInt(res.getColumnIndex("t2.EmpId"));
MyObjectAsProperty objProp2 = new MyObjectAsProperty();
objProp2 .EmpId = res.getInt(res.getColumnIndex("t2bId.EmpId"));
}while(res.moveToNext());
}
The documentation says:
The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next.
So you must use AS:
SELECT t1.Id AS ObjID, t2.EmpId AS EmpId, ...

Entity Framework query joins and group by issue

Please correct the query
IN PL/SQL
SELECT a.MENU_ID, a.menu_label, a.menu_value
FROM tbl_ims_menu a, TBL_IMS_ROLE_ASSIGNED_MENU b,TBL_IMS_USER_ROLE_PRIVILEGES c
WHERE a.menu_id = b.menu_id AND b.urole_id = c.granted_role
AND c.user_id = '3' AND a.menu_master <> '0'
AND a.menu_status = 'Active'
GROUP BY a.menu_id, a.menu_label, a.menu_value
query is working fine there is some issue when rewrite in Entity framework
check the following query
List<TBL_IMS_MENU> listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by m.MENU_ID
into g select g).ToList();
if I used Var instead of List then how to fire loop?
I think you need to remove your join statements - and just use the where like you do in raw SQL query:
var qry = (from a in db.TBL_IMS_MENU
from b in db.TBL_IMS_ROLE_ASSIGNED_MENU
from c in db.TBL_IMS_USER_ROLE_PRIVILEGES
where c.USER_ID == UserID
where b.UROLE_ID == c.GRANTED_ROLE
where a.MENU_ID == b.MENU_ID
where a.menu_status == "Active"
where a.menu_master != "0"
select a)
.GroupBy(c => c.menu_id)
.ThenBy(c => c.menu_label)
.ThenBy(c => c.menu_value)
.ToList();
Try something like this:
var listSubMenu = (from m in db.TBL_IMS_MENU
join ra in db.TBL_IMS_ROLE_ASSIGNED_MENU on m.MENU_ID
equals ra.MENU_ID
join rp in db.TBL_IMS_USER_ROLE_PRIVILEGES on ra.UROLE_ID
equals rp.GRANTED_ROLE
where rp.USER_ID == UserID
group m by new { m.MENU_ID, m.menu_label, m.menu_value }
into g select g).ToList();
foreach(var groupItem in listSubMenu)
{
// go through groups like this - groupItem.Key.MENU_ID
foreach(var menuItem in grouItem)
{
//go through each item in group like this - menuItem.GRANTED_ROLE
}
}

LINQ to SQL get one-to-many table column result as array

I have one table[T1] with this informaions
ID
UserID
Other...
2nd table[T2] is
ID
UserID
Name
Relationship is one user from T1 can have many T2
I want to get result like
ID = number
UserID = number
array[t2.name,t2.name,t2.name]
My ling to sql is like
var result = (from t1 in context.t1
join t2 in context.UserID on t1.ID equals t2.UserID
select new CLASS
{
ID = t1.ID,
UserID = t1.UserID,
Names = t2.name
}).Take(10).ToList();
But this give me result as each t2.name as separated row. How i can gather all names in array?
LINQ to SQL , ASP.NET C# 4.0
I think something like this should work
var result = (from t1 in context.t1
join t2 in context.UserID on t1.ID equals t2.UserID
select new CLASS
{
ID = t1.ID,
UserID = t1.UserID,
Names = (from t2 in context.t2
select t2.Name
where t2.userID = t1.userID).toArray(),
}).Take(10).ToList();
hope it helps
You don't need to use the join. Create a relationship b/n your two linq entites in the linq designer and then you can run a query like this:
var q = from t in context.t1
select new { id = t1.id, names = t1.t2s.Select(t => t.Name).ToArray() };
You have to group your results by ID/UserID:
var result = (from t1 in context.t1
join t2 in context.UserID on t1.ID equals t2.UserID
group by new { t1.ID, t1.UserID } into g
select new CLASS
{
ID = g.Key.ID,
UserID = g.Key.UserID,
Names = g.ToArray()
}).Take(10).ToList();
However, when LINQ to SQL classes are prepared right that kind of queries can be made like:
var result = from t1 in context.t1 select new { id = t1.id, names = t1.t2s.ToArray() };

Resources