LINQ grouping and ignoring column - c#

I'm using EF and LINQ.
I have the following in my db table
branchId Name ItemId CategoryId
2 Test1 1 1
5 Test1 1 1
3 Test1 1 2
2 Test2 2 1
7 Test2 2 1
I need to group by ItemId and BranchId should be ignored, so the output should be
Name ItemId CategoryId
Test1 1 1
Test1 1 2
Test2 2 2
Please help. thanks

You need to apply group by which is on multiple column, so for that you need to go for code like as below which do group by on multiple columns....
var numberGroups = from it in context.items
group it by new { it.ItemId,it.Name,it.CateogryID } into g
select new { ItemID = g.Key.ItemID, Name= g.Key.Name
CategoryID = g.Key.CategoryID };

var query = from item in context.Item
group by item.Name,ItemId,item.CategoryId
select item.Name,Item.Id,item.CategoryId;

Related

How to Update Column based on another table column value

I have two data tables
table
id isfav
---------
1 1
2 1
3 1
4 1
5 1
favtable id
-----------
2 true
3 false
So I want to update the table1 column isFav to 0 if the ids exist in FavTable with false.
Can anybody help me with this?
You can use Any() to search in other entities.
var db = new YourDbContext();
var favtable = db.favtable.ToList();
//Find them:
var result = db.Table1.Where(t => favtable.Any(f => f.id == t.id && !f.isfav));
//result should be 3.
.NET Fiddle: https://dotnetfiddle.net/BmaqN5

using group by in IEnumerable result

I have a query like below :
IEnumerable<qryTable1> templates = from t in db.qryTable1
where (t.GID == 1&&
t.RID == 4 && t.CID == "user")
select t;
Let say, Above query returns following result:
TId GId RId CID
1 1 1 a
1 1 2 a
2 1 1 a
2 1 2 a
3 1 1 a
3 1 2 a
Now I want to get result from the above query like below:(removing RId column so that result have duplicate entries having only TId,GId,CID and then group the result by TId).
TId GId CID
1 1 a
2 1 a
3 1 a
Also , I want to get the desired output in template object only.It means result should be in 'IEnumerable<qryTable1>' object.
var distinct = db.qryTable1.Select(r => new {Tid = r.TId, GId = r.GId, CId = r.CId})
.Distinct();

Linq - select N-level childs from hierarchy parents

I have 2 tables as below:
Parent child relationship (table 1):
SourceId SourceParentId
1 null
2 1
3 2
4 null
Items (Table 2):
Id SourceId
1 1
2 1
3 2
4 2
5 3
6 4
How to write a linq query that return me all items based on Source ID? If my input is SourceId = 1, i will get items 1,2,3,4 & 5. If my input for sourceId is 2, i will get 3 items: 3, 4 & 5. If my input for sourceID is 4, it will return me item 6. My parent child is N-level and items can appear at any level.
Help :(
Here try this
--Variable to hold input value
DECLARE #inputSourceID INT = 1;
--Recursive CTE that finds all children of input SourceID
WITH MyCTE
AS
(
SELECT SourceID,
SourceParentID
FROM table1
WHERE SourceID = #inputSourceID
UNION ALL
SELECT table1.SourceID,
table1.SourceParentID
FROM table1
INNER JOIN MyCTE
ON table1.SourceParentID = MyCTE.SourceID
)
--Join the CTE with the table2 to find all id
SELECT table2.ID
FROM MyCTE
INNER JOIN table2
ON MyCTE.SourceID = table2.SourceID

EF Sum between 3 tables

Say we got a Database design like this.
Customer
Id Name
1 John
2 Jack
Order
Id CustomerId
1 1
2 1
3 2
OrderLine
Id OrderId ProductId Quantity
1 1 1 10
2 1 2 20
3 2 1 30
4 3 1 10
How would I create an entity framework query to calculate the total Quantity a given Customer has ordered of a given Product?
Input => CustomerId = 1 & ProductId = 1
Output => 40
This is what I got so far, through its not complete and still missing the Sum.
var db = new ShopTestEntities();
var orders = db.Orders;
var details = db.OrderDetails;
var query = orders.GroupJoin(details,
order => order.CustomerId,
detail => detail.ProductId,
(order, orderGroup) => new
{
CustomerID = order.CustomerId,
OrderCount = orderGroup.Count()
});
I find it's easier to use the special Linq syntax as opposed to the extension method style when I'm doing joins and groupings, so I hope you don't mind if I write it in that style.
This is the first approach that comes to mind for me:
int customerId = 1;
int productId = 1;
var query = from orderLine in db.OrderLines
join order in db.Orders on orderLine.OrderId equals order.Id
where order.CustomerId == customerId && orderLine.ProductId == productId
group orderLine by new { order.CustomerId, orderLine.ProductId } into grouped
select grouped.Sum(g => g.Quantity);
// The result will be null if there are no entries for the given product/customer.
int? quantitySum = query.SingleOrDefault();
I can't check what kind of SQL this will generate at the moment, but I think it should be something pretty reasonable. I did check that it gave the right result when using Linq To Objects.

Linq query help

Is there a way to write a Linq statement to find duplication in Column B, and only find it when Column A has duplicated values then add the value of Column B to the Column where the duplication is found. Any help is appreciated thanks.
RecordID CartID Quantity ProductID
1 11 3 3
2 12 5 6
3 11 6 3
Delete record 3 and add 6 to the Quantity of RecordID 1 so that it becomes:
RecordID CartID Quantity ProductID
1 11 9 3
2 12 5 6
var records = (from i in list
group i by i.CartID into g
select new Item()
{
RecordID = g.Min(o => o.RecordID),
CartID = g.Key,
Quantity = g.Sum(o => o.Quantity),
ProductID = g.Min(o => o.ProductID)
}).ToList();
This sums all the quantity of items with the same CartId creating only the min occurent RecordId and ProductId as you asked. Selecting the min ProductId is something I needed to do to make the query work.
That is why I think you miss some grouping on ProductId...
You did not ask for this, but I think this is what you want (because it makes common sense to not group apples and pears together). (It gives the same result on the sample data provided but for different ProductsIds it will have different results.
var records = (from i in list
group i by new { cartID = i.CartID, prodID = i.ProductID } into g
select new Item()
{
RecordID = g.Min(o => o.RecordID),
CartID = g.Key.cartID,
Quantity = g.Sum(o => o.Quantity),
ProductID = g.Key.prodID
}).ToList();
This groups by CartID and ProductId. Multi-field grouping in Linq is achieved with anonymous types.

Categories