how to ignore/avoid/skip child collection from a LINQ query? - linq

I have a collection :
order
o1
o2
o3
I have orderItem collection :
OrderItem orderId
oitem1 o1
oitem2 o1
oitem3 o1
oitem4 o2
oitem5 o3
now I am selecting orders with line, but how do I skip/avoid/ignore the child collection from LINQ
I don't want orderItems with my orders .

Related

How to query names from a record with multiple IDs in LINQ

I have a table [A] that has columns such as CreatedBy(ID), AuthorizedBy(ID), SentTo(ID) and I need to join them to a table [B] containing user names (UserID, FullName). How can I write a join that connects each record of table A to multiple records in table B to fill in the CreatedBy/AuthorizedBy/SentTo names using LINQ?
can give try as below , basically you have to join B with A three times
form a in A
join b in B on b.Id = a.Createdby
join b1 in B on b1.Id = a.Authrizedby
join b2 in B on b2.Id = a.SentTo
select new {
a.Id,
CreatedBy= b.FullName,
AuthorizedBy = b1.FullName,
SentTo= b2.FullName};
or
from a in A
select new {
a.ID
CreatedBy= b.FirstOrDefault(a.CreatedBy== b.Id).FullName,
AuthorizedBy = b.FirstOrDefault(a.AuthorizedBy== b.Id).FullName,
SentTo= b.FirstOrDefault(a.SentTo== b.Id).FullName
}

LINQ Equivalent of and SQL query with two inner joins and a left join

I would be grateful for help with a LINQ equivalent of the following SQL Query (which works). Below this SQL query I give some description of my simple data base and the problem I want to solve.
Select a.Name, a.OrderID,
b.ProductIDFirst, c1.productName ProductNameFirst,
b.ProductIDSecond , c2.productName ProductNameSecond
from Customers a
INNER JOIN ORDERS b ON a.OrderID = b.OrderID
left join products c1 on b.productidfirst = c1.productid
left join products c2 on b.ProductIDSecond = c2.productid
Background information on the database structure:
I have a simple SQL Server Database with three Tables named Products, Orders and Customers.
The business model is such that each order can have only two products (not more).
The Orders table has two foreign keys, though they both come from the Products table. These Foreign Key field Names in the Orders Table are ProductIDFirst and ProductIDSecond. These two Foreign Keys in the orders table correspond to two products that each order can have. Customers table has one Foreign Key which comes from the Orders Table.
Now I need help with an LINQ query that will return me all customers such that I get five fields - CustomerName, OrderID and Names of each of the two products that match the OrderID in the customer product.
Your links are non-existent, so here's a best attempt without seeing anything.
Assuming you have the following containers (you'll need to change them for your scenario):
var customers = new List<Customer>();
var orders = new List<Order>();
var products = new List<Product>();
You can do the following:
var query =
from a in customers
join b in orders
on a.OrderId equals b.OrderId
join c1 in products
on b.ProductIdFirst equals c1.ProductId into c1a
join c2 in products
on b.ProductIdSecond equals c2.ProductId into c2a
from p1 in c1a.DefaultIfEmpty()
from p2 in c2a.DefaultIfEmpty()
select new
{
Name = a.Name,
OrderId = a.OrderId,
ProductIdFirst = p1 == null ? null : p1.ProductIdFirst,
ProductNameFirst = p1 == null ? null : p1.ProductNameFirst,
ProductIdSecond = p2 == null ? null : p1.ProductIdSecond,
ProductNameSecond = p2 == null ? null : p1.ProductNameSecond,
};
In short, where you want a left join, project the join into something else (e.g. c1a, c2a) then call from on them using DefaultIfEmpty() which will set null when no matching item exists on the right-hand-side.

Linq query to order by on multiple related data

Consider the below tables with the data
Customers -> Orders -> Items
Customers ->
A
B
C
Orders ->
o1 - A,
o2 - A,
o3 - B,
o4 - C
OrderItems ->
o1 - Item1,
o1 - Item2,
o2 - Item3,
o3 - Item2,
o4 - Item1
Item ->
Item1,
Item2,
Item3,
Item4
We have a similar mapping as above in our DB.
Now in linq i would like to get List of Customers sorted by Items which are comma seperated
eg:
Customer Items
C Item1
A Item1, Item2
B Item2
Ive tried something like this
Customer.OrderBy( cust => string.Join(",", cust.Orders
.SelectMany( order=>order.OrderItems)
.Select( orderItem=> orderItem.Item.Name)
.OrderBy(item=>item)));
but string.Join is not allowed inside linq statements..
Its not required to display the Items in my grid, but i need to get customers sorted by the comma separated Items..
And also i dont want this to be done in the UI level as the sorting needs to be done on IQueryable customer object to which other filters are added and then executed later ..
A linq orderby query with IQueryable Customer object, returning an IQueryable object.
When you are querying the Customer collection, your query will be converted to sql instructions. Since you mentioned other filters, I suppose it's a big table and you don't want to display / get all rows.
Even the string.Join operator were allowed in EF, your order clause operates on an complex statement that needs to be processed for each row to determine the correct result order (see this question for an example of what your string.join would do).
You need to somehow simplify your order clause or store the item list string into an sql field. If you can't store this string, you can try to create a stored procedure, a view or process the data using linq to objects (in this case, apply all filters using EF, get the results using .ToList() and apply your order filter using regular linq to objects).
Be aware that if you don't simplify your query and the customer table is big, you'll face some performance issues.

Linq multiple join on same table

how can i write this query in linq?
select * from bills as b inner join customer as c1
On b.shipperID=c1.CustomerID inner join customer c2
On b.ConsigneeID=c2.CustomerID
---------------------------
I need to have it as below:
var result=from p1 in entities.bills
join p2 in entities.customer on p1.shipperID equals p2.customerID
join p3 in entities.customer on p1.consigneeID equals p3.customerID
select p2;
return resuls.Tolist()
Thanks:)
In your SQL you are selecting all so in linq you need to put all objects in your new anonymous type as below.
var result = from p1 in entities.bills
join p2 in entities.customer on p1.shipperID equals p2.customerID
join p3 in entities.customer on p1.consigneeID equals p3.customerID
select new
{
Bills = p1,
Shippers = p2,
Consignees = p3
};
return resuls.Tolist();
or if you need them flattened you'll had to project them property by property.
You ought to use navigation properties in LINQ, something like
from bills in entities.bills
select new
{
bills.Shipper
bills.Consignee
};

How to fetch records using predicate from A having relationship with A<-->B<-->C with coredata

I have three entities A,B,C.
EntityA:
Properties:
Id,Name
Relationships:
ABRelation(A-->>B)
EntityB:
Properties:
Id,Name
Relationships:
BARelation(B-->A)
BCRelation(B-->>C)
EntityC:
RoleId
CBRelation(C-->>B)
Now I need to fetch records from Entity A having some RoleId = 23 which is contain in C.
Could you please help me quickly.Thanks in Advance.
SELECT a.Id, a.Name,
FROM EntityA a INNER JOIN EntityB b
ON a.Id= b.EntityA_Id(foriegn key) JOIN EntityC as c
ON b.EntityC_Id(foriegn key) = c.Id
WHERE c.RoleId=23

Resources