I have this query:
var rowsPerProvider = (from row in dt.Select()
let emp = row["f_name"].ToString().Trim()
group row by emp
into g
select g).ToDictionary(
g => g.Key,
g => g.ToArray());
How can I update it to also filter on some more columns? for example currently it is on f_name. How can I update it to group on f_name and m_name and l_name?
Make an anonymous object containing the fields you want to group by:
var rowsPerProvider = (from row in dt.Select()
group row by new
{
emp1 = row["f_name"].ToString().Trim(),
emp2 = row["m_name"].ToString().Trim(),
emp3 = row["l_name"].ToString().Trim(),
}
into g
select g).ToDictionary(
g => g.Key,
g => g.ToArray());
Use anonymous class:
// (...)
group row by new { emp, something }
Related
I want to use that query in ef:
select count(number) as CountOfNumber ,number, name from table_1 group by number, name order by CountOfNumber desc
How can I use count on column and add this column new name.
You need to use GroupBy, OrderBy and Count combination:
var results = (from item in db.table_1
group item by new { item.name, item.number } into grouping
orderby grouping.Count() descending
select new
{
name = grouping.Key.name,
number = grouping.Key.number,
CountOfNumber = grouping.Count()
}).ToList();
https://msdn.microsoft.com/en-us/library/bb545971.aspx
You can use the Count method on the group. Where context is your Entity Framework data context or any IEnumerable.
var results = from t in context.table_1
group t by new { t.number, t.name } into g
orderby g.Count() descending
select new { number = g.Key.number, name = g.Key.name, count = g.Count() };
I have some linq which returns a list of properties and the number of bookings for a given year. However if a property has no bookings then it is not included in the resultset.
var bookings = from b in db.Bookings
orderby b.PropertyId
where b.StartDate.Year == Year
group b by b.Property.Title into grp
select new { key = grp.Key, cnt = grp.Count() };
How can this be changed to include properties with no bookings?
I'm assuming there's a Properties table based on your code. You need to select from Properties instead:
var bookings = from p in db. Properties
orderby p.Id
group p by p.Title into grp
select new
{
key = grp.Key,
cnt = grp.Count(p => p.Bookings.Where(b => b.StartDate.Year == Year))
};
I guess you should filter on year in the count, then
var bookings = from b in db.Bookings
orderby b.PropertyId
group b by b.Property.Title into grp
select new {
key = grp.Key,
cnt = grp.Count(x => x.StartDate.Year == Year)
};
I have this Linq query:
from i in data.Items
join tdi in data.TDItems on i.itemId equals tdi.itemId
group i by i.ItemId
into selection
select new
{
itemId = selection.Key
number = selection.Sum(x => x.quantity) // quantity is a field in TDItems
}
How do I create this sum function? because I'm grouping by an attribute in the Items table, I can't call a Sum on the TDItems table.
group new { i, tdi } by i.ItemId
...
select new
{
selection.Sum(x => x.tdi.quantity)
}
from c in (from i in #as
join tdi in bs on i.itemId equals tdi.itemId
select new
{
itemId = i.itemId,
quantity = tdi.quantity
})
group c by c.itemId
into selection
select new
{
itemId = selection.Key,
number = selection.Sum(x => x.quantity) // quantity is a field in TDItems
};
I have the following LINQ query:
var query =
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
select new { MyObjectID = g.Key, totalSum })
.Max(g => g.totalSum);
I want to select both the object id and price of the object with the maximum price. How can I do that?
Use an order by descending clause and call FirstOrDefault().
(from p in obj1
group p by p.objID into g
let totalSum = g.Sum(p => p.ObjPrice)
orderby totalSum descending
select new { MyObjectID = g.Key, totalSum }).FirstOrDefault();
I want to use LINQ to group data from a DataTable (columns: userid, chargetag, charge).
The content could look like this:
userid chargetag charge
-----------------------------
user1 tag3 100
user2 tag3 100
user3 tag5 250
I need something like this as a result:
chargetag count sum
-------------------------
tag3 2 200
tag5 1 250
This is what I have so far:
var groupedData = from b in dataTable.AsEnumerable()
group b by b.Field<string>("chargetag") into g
let count = g.Count()
select new
{
ChargeTag = g.Key,
Count = count,
};
I can extract the name of the chargetag and the number of it.
How would I have to change the LINQ query to access the sum of charges as well?
Thanks in advance :-)
Regards,
Kevin
That's pretty easy - just use the Sum extension method on the group.
var groupedData = from b in dataTable.AsEnumerable()
group b by b.Field<string>("chargetag") into g
select new
{
ChargeTag = g.Key,
Count = g.Count(),
ChargeSum = g.Sum(x => x.Field<int>("charge"))
};
(I've removed the let clause here as it wasn't really buying you anything.)
Now that may be inefficient; it may end up grouping twice in order to perform two aggregation operations. You could fix that like with a query continuation like this, if you really wanted:
var groupedData = from b in dataTable.AsEnumerable()
group b by b.Field<string>("chargetag") into g
select new
{
ChargeTag = g.Key,
List = g.ToList(),
} into g
select new
{
g.ChargeTag,
Count = g.List.Count,
ChargeSum = g.List.Sum(x => x.Field<int>("charge"))
};
Or with a let clause instead:
var groupedData = from b in dataTable.AsEnumerable()
group b by b.Field<string>("chargetag") into g
let list = g.ToList()
select new
{
ChargeTag = g.Key,
Count = list.Count,
ChargeSum = list.Sum(x => x.Field<int>("charge"))
};