Linq. Select from multiple tables - c#

In project I have this tables:
Product(id,catalogId, manufacturerId...)
Catalog
Manufacturer
Also Product model (id, name, catalogId, catalogTitle, manufacturerId, manufacturerName).
How can write in Linq this SQL query below if I want get Product item?
SELECT Product.Name, Product.CatalogId, Product.ManufacturerId, [Catalog].Name, Manufacturer.Name
FROM Product, [Catalog], Manufacturer
WHERE [Catalog].Id=Product.CatalogId AND Manufacturer.id=Product.ManufacturerId AND Product.Active=1

First, I'll answer your question.. then address your answer to comments. To answer your question, in Linq you would do the following:
from p in Product
join c in Catalog on c.Id equals p.CatalogId
join m in Manufacturer on m.Id equals p.ManufacturerId
where p.Active == 1
select new { Name = p.Name, CatalogId = p.CatalogId, ManufacturerId = p.ManufacturerId, CatalogName = c.Name, ManufacturerName = m.Name };
This will give you an anonymous object with the items you requested. If you need to use this elsewhere (and you're not using dynamic objects), I would suggest creating a view-model, and instantiating one of those in your select.
Example:
public class ProductInfoView
{
public string Name { get; set; }
public int CatalogId { get; set; }
public int ManufacturerId { get; set; }
public string CatalogName { get; set; }
public string ManufacturerName { get; set; }
}
from p in Product
join c in Catalog on c.Id equals p.CatalogId
join m in Manufacturer on m.Id equals p.ManufacturerId
where p.Active == 1
select new ProductInfoView() { Name = p.Name, CatalogId = p.CatalogId, ManufacturerId = p.ManufacturerId, CatalogName = c.Name, ManufacturerName = m.Name };
This will make referencing your query results a little less painful.
To answer your comment, you're doing a lot of joins if all you want is the product. Your criteria will only ensure three things
Your product's Active flag is 1
Your product has an existing Catalog entry
Your product has an existing Manufacturer entry
If #2 and #3 are superfluous and you don't necessarily need the names, you could simply do:
from p in Product
where p.Active == 1
select p
If Product is a CRUD model, you could potentially deep-load it to include Manufacturer/Catalog information, or use the aforementioned view-model.
Good luck!

To combine results from multiple tables without explicitly joins:
from p in Product
from c in Catalog
from m in Manufacturer
where c.Id == p.CatalogId && m.Id == p.ManufacturerId && p.Active == 1
select new
{
p.Name,
p.CatalogId,
p.ManufacturerId,
c.Name,
m.Name
};

Related

How to optimize this query using EF

Hi good day I am new with Entity Framework. I just wanna to know if there is a way I could improve my implementation. Here are the codes.
public async Task<List<Record>> GetRecordsByBatchId(string batchId, string source)
{
List<string> idList = new List<string>();
//[1] Get all parent ID from table 1 with a filter of source and batchId
var parentIds= await _context.Set<FirstTable>()
.Where(a => a.IsActive
&& a.BatchId.Equals(batchId)
&& a.Source.Equals(source)).Select(b => b.ParentId).ToListAsync();
if (parentIds.Count() == 0)
{
return new List<Record>();
}
//[2] Query idNumber of each parentId from [1] to SecondTable
List<long> idNumber = await _context.Set<SecondTable>()
.Where(a => parentIds.Contains(a.Id))
.Select(b => b.IdNumber).ToListAsync();
//[3] Query Record/s that contains idNumber from previous query [2]. it is possible that 1 or
//more records has same idNumber
List<Risk> recordByIdNumber = await _context.Set<SecondTable>()
.Where(a => idNumber.Contains(a.IdNumber)).ToListAsync();
//[4] In this part I just want to group the records in [3] by Id number and sort each group
//by its endorsementNumber in descending order and return the record with highest endorsement
//number for each group
return (from record in recordByIdNumber
group record by record.IdNumber into g
orderby g.Key
select g.OrderByDescending(risk =>risk.EndorsementNumber).FirstOrDefault()).ToList();
}
}
The model for the FirstTable
public class FirstTable
{
public Guid? ParentId{ get; set; }
public string BatchId { get; set; }
public string Source { get; set; }
public bool IsActive { get; set; }
}
The model for the SecondTable
public class SecondTable
{
public Guid Id{ get; set; }
public int EndorsementNumber { get; set; }
public long IdNumber { get; set; }
}
Note: I just include the necessary properties in the model.
This approach is working as expected. I just wanna know if there is a possibility that these queries could be optimized that there is only 1 query for the SecondTable table.
Any help would be greatly appreciated, thanks in advance.
Yes, queries 1-3 can and should be combined. In order to do that you need, to have navigation properties in your model. It seems that there is one-to-many relationship between FirstTable and SecondTable. Let's use Customer and Order instead.
class Customer {
int CustomerId
string BatchId
ICollection<Order> Orders
}
class Order {
int OrderId
int CustomerId
Customer Customer
Risk Risk
}
in which case you just write third query as
List<Risk> = await _context.Orders.Where(o => o.Customer.BatchId == batchId)
.Select(o => o.Risk).ToListAsync();
Obviously, I am only guessing the structure and the relationship. But hopefully, this can get you started. For me Contains() is "code smell". There is a high chance that there will be large list out of your first query, and contains() will produce a huge IN clause in the database, that can easily crash the system
var parentIds = _context.Set<FirstTable>()
.Where(a => a.IsActive
&& a.BatchId.Equals(batchId)
&& a.Source.Equals(source)).Select(b => new { b.parentId });
var risks = await (from s in _context.Set<SecondTable>()
join p in parentIds on s.Id equals p.parentId
join r in _context.Set<SecondTable>() on s.IdNumber equals r.IdNumber
select r).GroupBy(r=>r.IdNumber)
.Select(r=> r.OrderByDescending(risk =>risk.EndorsementNumber).FirstOrDefault())
.ToArrayAsync();
return risks;
You can have 1 query instead of 3. It will perform better as the number of the rows from the first query grows.
EDIT: As #SvyatoslavDanyliv mentioned in the comments, group-take operations may not work depending on the version of the EF and the provider you use. You may need to separate the query and the group by operation like below :
var result = await (from s in _context.Set<SecondTable>()
join p in parentIds on s.Id equals p.parentId
join r in _context.Set<SecondTable>() on s.IdNumber equals r.IdNumber
select r).ToArrayAsync();
var risks = result.GroupBy(r=>r.IdNumber)
.Select(r=> r.OrderByDescending(
risk =>risk.EndorsementNumber).FirstOrDefault())
.ToArray();
return risks;

LINQ Query - Display customers with their number of orders including the customer with no orders

In the following LINQ Query I need to display All the customers with total number of their orders placed including the customers that have not placed any orders:
Models:
Public class Customer
{
public int CustomerId { get; set; }
public string Name{ get; set; }
}
Public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
}
LINQ Query: Question: How can I display ALL customers (including the one that have no order) and total number of orders per customer (zero for those whose customerID is not in Orders table)
var Query1 = from c in Customers
join o in Orders into co
from t in co.DefaultIfEmpty()
select new {CustomerID = c.CustomerId, OrderID = (t == null ? 0 : t.OrderId)};
You basically need to do a LEFT JOIN between your Customer table and Customer Order table and then do a group by on that result to count the orders for each customer.
Assuming you have a class like this
public class CustomerOrder
{
public int CustomerId { set; get; }
public int? OrderId { set; get; }
}
This class is to store each item for the left join result
Now, you need to first do a LEFT JOIN and project the result of that to a list of CustomerOrder class objects, Then do a GroupBy on top of that
var usersWithCount = (from c in db.Customers
join o in db.Orders on c.CustomerId equals o.CustomerId
into result
from sub in result.DefaultIfEmpty()
select new CustomerOrder{ OrderId= sub!=null ? sub.OrderId :(int?) null,
CustomerId = u.CustomerId }
) // Here we have the left join result.
.GroupBy(g => g.CustomerId , (k, orders) => new {
CustomerId = k,
OrderCount = orders.Count(t=>t.OrderId !=null)}).ToList();
The result stored in usersWithCount will be a collection of annonymous object with a CustomerId and OrderCount property.
This isn't very efficient since it iterates through Orders for every customer, but it'll get the job done:
var query = Customers
.Select(c => new
{
Name = c.Name,
NumOrders = Orders.Count(o => o.CustomerId = c.CustomerId)
});
foreach (var result in query)
Console.WriteLine("{0} -> {1}", result.Name, result.NumOrders);
var Query1 = from c in Customers
join o in Orders on c.CustomerId equals o.CustomerId into OrdersGroup
from item in OrdersGroup.DefaultIfEmpty(new Order { OrderId = 0, CustomerId = 0 })
select new {CustomerID = c.CustomerId, OrderID = (item == null ? 0 : item.OrderId)};
this will return all customers (even if they have no orders ) and orders
https://dotnetfiddle.net/BoHx2d

LINQ to SQL - Left OUTER Join with two joins not working

In the following LINQ Query I need to display All the customers including the customers that have not placed any order but whose order is priced more than $100. But my following LINQ Query is returning all the customers regardless of their order price. It seems to be ignoring my Where(ord => ord.price > 100) clause in the LINQ query below. What I may be doing wrong?
Models:
public class Customer
{
public int CustomerId { get; set; }
public string CustName{ get; set; }
}
public class Order
{
public int OrderId { get; set; }
public int CustomerId { get; set; }
public float price { get; set; }
}
LINQ Query:
var Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord into cord
from t in cord.Where(ord => ord.price > 100).DefaultIfEmpty()
select new {CustName= c.Name, OrderID = (t == null ? 0 : t.OrderId)};
SQL Query:
I want to translate following T-SQL query into LINQ query:
SELECT c.Name, OrderID
FROM Customers c
LEFT OUTER JOIN Orders ord
ON c.CustomerID = ord.CustomerID
AND ord.Price > 100
Well, I don't have your data to be able to verify this, but one issue that jumps out at me is you're not specifying the field (CustomerId) on which to join the two collections.
Try modifying your query like this: (adding on c.CustomerId equals ord.CustomerId)
var Query1 = from c in Customers
join ord in Orders on c.CustomerId equals ord.CustomerId into cord
from t in cord.DefaultIfEmpty()
where t.price > 100
select new {CustName = c.Name, OrderID = (t == null ? 0 : t.OrderId)};

EF Core relationships query

I'm fairly new to Entity Framework, my tables relationship looks a bit like this
public class Customer {
public int Id { get; set; }
public string Name { get; set; }
public List<Product> Products { get; set; }
}
public class Product {
public int Id { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
I would like to make a query on the Customer table and include only the last Product created MAX(Id)
Normal SQL query would look like this
SELECT *
FROM Customer
INNER JOIN Product ON Customer.Id = Product.CustomerId
WHERE Product.Id = (SELECT MAX(Id) FROM Product WHERE CustomerId = Customers.Id)
My current EF query look like this but it return all the products...
List<Customer> customers = _context.Customers
.Include(c => c.Products)
.ToList();
I tried something like this which gives me the right results, but EF makes a bunch of query and very quickly I see this seems like wrong way to go at it
List<Customer> customers = _context.Customers
.Select(c => new Customer() {
Id = c.Id,
Name = c.Name,
c.Products = c.Products.Where(d => d.Id == c.Products.Max(max => max.Id)).ToList()
}).ToList();
I would like some suggestion, or if there's a different way to make this works.
It looks like below query can be written in a different way
SELECT *
FROM Customer
INNER JOIN Product ON Customer.Id = Product.CustomerId
WHERE Product.Id = (SELECT MAX(Id) FROM Product WHERE CustomerId = Customers.Id)
This can be written as
SELECT TOP 1 *
FROM Customer
INNER JOIN Product ON Customer.Id = Product.CustomerId
Order by Product.Id desc
Assuming customer name is required,above query can be written in LINQ or using EF as below
var customers = _context.Customers.Join(_context.Products, cu => cu.id,
p => p.CustomerId, (cu,p) => new { cu,p})
.Select( c => new { prodId = c.p.Id,customername = c.cu.Name })
.OrderByDescending( c => c.prodId).Take(1);
If you have configured navigation property 1-n I would recommend you to use:
var customers = _context.Customers
.SelectMany(c => c.Products, (c, p) => new { c, p })
.Select(b => new { prodId = b.p.Id, customername = b.c.Name })
.OrderByDescending(c => c.prodId).Take(1);
Much more clearer to me and looks better with multiple nested joins.

Linq - Join where ID's !=, select new + distinct?

I have the following classes.
Course;
public class Course
{
//pk
public int Id{ get; set; }
public int SourceCourseId { get; set; }
public string Name { get; set; }
}
Registration
public class Registration
{
//primary key
public int Id { get; set; }
//...more fields
public int CourseId { get; set; }
}
I want to obtain a collection of annonymous objects with the two fields below for all Courses that are Distinct in the registrations table that are not in the Courses table.
var distinctCourses = (from registration in db.Registrations
join courses in db.Courses on registration.CourseId equals courses.SourceCourseId
where registration.CourseId != courses.SourceCourseId
select new
{
SourceCourseId = registration.CourseId,
Name = registration.CourseName,
}).Distinct().ToList();
For some reason the above is returning 0... Any suggestions?
try a left join:
var query = from r in registrations
join c in courses on r.CourseId equals c.id into newCourses
from nullCourse in newCourses.DefaultIfEmpty()
where nullCourse == null
select new { }
Edit - per comment from Alex :
Also, your where clause needs to change to
where nullCourse == null
Edit - changed join columns and added correct where clause.
Edit - group registrations on CourseID so they will be distinct
var distinctCourses =
(from registration in db.Registrations
group registration by registration.CourseId into grp
from reg in grp
join courses in db.Courses on reg.CourseId equals courses.SourceCourseId into newCourses
from nullCourse in newCourses.DefaultIfEmpty()
where nullCourse == null
select new
{
SourceCourseId = reg.CourseId,
Name = reg.CourseName,
}).ToList();
Try this
var result = Registrations.GroupJoin(Courses,r=>r.CourseId,c=>c.SourceCourseId,
(k,g) => new {k,g})
.Where(x=>x.g.Count()==0)
.Select(s=> new {id=s.k.CourseId,name=s.k.CourseName});

Categories