WHERE clauses in JOIN using LINQ with lambdas - asp.net

I have this situation:
I have a form in ASP.NET and I need to extract data from a mssql db. The LINQ query is build from the values that are inserted in the form.
if (ddlRegion.SelectedIndex > 0)
{
query = query.Where(re => re.Region == ddlRegion.SelectedValue);
}
if (tbName.Text.Trim().Length > 0)
{
query = query.Where(na => na.Name.Contains(tbName.Text));
}
var result = query.Select(res => new
{
res.ColumnA,
res.ColumnB,
res.ColumnC
});
The problem is that I need to make a join with TableB
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => tA);
The original SQL command is like this:
select tA.ColumnA, tA.ColumnB, tA.ColumnC from TableA tA join TableB tB on tA.Code=tB.Code where tB.ExpireDate>=getdate() and tB.datavalabil >=getdate()
The problem is where clauses from table tB join.

You can do something like this:
query = query.Join(TableB, tA => tA.Code, tB => tB.CodFiscal, (tA, tB) => new { tA, tB })
.Where(x => x.tB.ExpireDate >= DateTime.Now and x.tB.datavalabil >= DateTime.Now)
.Select(x => x.tA);
Or in query syntax:
query =
from tA in query
join tB in TableB on tA.Code equals tB.CodFiscal
where tB.ExpireDate >= DateTime.Now and tB.datavalabil >= DateTime.Now
select tA;

Related

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();

Core EF Outer Join,Count & Group

I'm trying to convert this SQL Query into Core EF:
SELECT w.IdShippingBatch, w.BookingNumber, COUNT(c.IdShippingOrder) AS ShippingOrders, w.CreatedOn, w.ModifiedOn
FROM dbo.Shipping`enter code here`Batch AS w LEFT OUTER JOIN
dbo.ShippingOrders AS c ON w.IdShippingBatch = c.IdShippingBatch
WHERE (w.IdCompany = 2) AND (w.IdDealer = 1)
GROUP BY w.IdShippingBatch, w.BookingNumber, w.CreatedOn, w.ModifiedOn
I have tried multiple solutions, including several here. My latest attempt looks like:
var data = (from w in _context.ShippingBatch
join c in _context.ShippingOrders on w.IdShippingBatch equals c.IdShippingBatch into t1
where w.IdCompany == idCompany && w.IdDealer == idDealer
from t2 in t1.DefaultIfEmpty()
group t2 by new { w.IdShippingBatch, w.BookingNumber, w.CreatedOn, w.ModifiedOn } into t3
select new ShippingBatchDTO
{
IdShippingBatch = t3.Key.IdShippingBatch,
BookingNumber = t3.Key.BookingNumber,
ShippingOrders = t3.Count(),
CreatedOn = t3.Key.CreatedOn,
ModifiedOn = t3.Key.ModifiedOn
});
I have also tried adding t3.count(m => m.something != null), but that throws an error.
One major point of EF is to map the relationship between entities so that you can leverage LINQ and let EF compose an SQL query rather than trying to replace SQL with LINQ-QL.
If your ShippingBatch is mapped with a collection of ShippingOrders...
var batches = _context.ShippingBatch
.Where(x => x.IdCompany == idCompany && x.IdDealer == idDealer)
.Select(x => new ShippingBatchDTO
{
IdShippingBatch = x.IdShippingBatch,
BookingNumber = x.BookingNumber,
ShippingOrders = x.ShippingOrders.Count(),
CreatedOn = x.CreatedOn,
ModifiedOn = x.ModifiedOn
}).ToList();
If your ShippingBatch does not have a collection of ShippingOrders, but your ShippingOrder reference an optional ShippingBatch.
var batches = _context.ShippingOrder
.Where(x => x.ShippingBatch != null
&& x.ShippingBatch.IdCompany == idCompany
&& x.ShippingBatch.IdDealer == idDealer)
.GroupBy(x => x.ShippingBatch)
.Select(x => new ShippingBatchDTO
{
IdShippingBatch = x.Key.IdShippingBatch,
BookingNumber = x.Key.BookingNumber,
ShippingOrders = x.Count(),
CreatedOn = x.Key.CreatedOn,
ModifiedOn = x.Key.ModifiedOn
}).ToList();
That should hopefully get you moving in the right direction. If not, expand your question to include details of what you are seeing, and what you expect to see along with definitions for the applicable entities.

In one Linq Query get back data from multiple tables

I have 3 tables:
sessions - This store information about trainings
xref_session_faculty - This cross references the trainings and the teacher
user - list of all teachers
In one(or more) LINQ query i want to get all the sessions and for each session the teachers that will be conducting the training. Each session can have zero or more teachers in the DB.
sessions = db.sessions
.Where(x => x.seminar_id == seminarId)
.ToList()
.Select((x, i) => new fees
{
id = x.id,
sessionTitle = x.title,
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).ToList()
)
)
})
.ToList();
With this the teacherNames prints out By:System.Collections.Generic.List1[System.String],System.Collections.Generic.List1[System.String].
WHat is the right query format here?
teacherNames = "By:" + String.Join(",",
x.xref_session_faculty.ToList()
.Select(q => db.users
.Where(m => m.id == q.user_id)
.Select(t => t.firstName).FirstOrDefault()
)
)
you need to change ToList to FirstOrDefault function to get correct result
private var sessions = (from session in db.sessions.Where(x => x.seminar_id == seminarId)
select new
{
id = session.id,
sessionTitle = session.title,
teacherNames = (from faculty in db.xref_session_faculty.
where (x => x.session_id == session.id)
join us in db.uses on faculty.user_id equals us.user_id
select new
{
us.firstName,
other_field_names
})
});

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
}
}

How do I create a followup LINQ .join() if an additional filter item is presented?

If I have a lystId, I want to include the MemberProductLyst object and filter by the LystId.
Any suggestions for the proper way to implement the follow up Lamba code inside of the
if (!string.IsNullOrEmpty(lystId)) {} block below the initial query???
products = (from p in dc.Products
join t in dc.Tags on p.TagId equals t.TagId
join pi in dc.ProductImages on p.ProductId equals pi.ProductId
join i in dc.Images on pi.ImageId equals i.ImageId
where p.IsActive == true
where t.TagId == new Guid(brandId)
orderby p.CreatedOn descending
select new ProductItem
{
productImage = i.FileName,
productId = p.ProductId,
description = p.Description,
name = p.Name,
adImage = t.AdImage
}).Skip(totalItemCount).Take(pageSize);
if (!string.IsNullOrEmpty(lystId))
{
//Include MemberProductLyst table to Filter by lystId if LystId is available
var memberLysts = from mpl in dc.MemberProductLysts
where mpl.LystId == new Guid(lystId)
select new { mpl.LystId, mpl.ProductId };
products = (IQueryable<ProductItem>)products.Join(memberLysts, p => p.productId, mpl => mpl.ProductId, (p, mpl) => new {ProductItem = p, MemberProductLyst = mpl });
}
It largely depends on the intent of your Join, but I suspect this may yield the results you're looking for:
products = products.Where(
p => memberLysts.Any(mpl => mpl.ProductId == p.productId));

Resources