LINQ to DataSet group by with specific column - c#

I have a dataset with following table
id domain email
1 google.com av#et.org
2 apple.com gopal#jt.com
3 abc.com av#et.org
4 global.com av#et.org
5 local.com gopaljite#gmail.com
6 xyz.com gopaljite#gmail.com
7 gpl.com gopal#jt.com
8 mno.com av#et.org
9 pqr.co.in gopaljite#gmail.com
10 aad.com av#et.org
Now I need output as following
Sr. Email Domain_Count
1 av#et.org 5
2 gopal#jt.com 2
3 gopaljite#gmail.com 3
How can I do this with LINQ ?
Thanks in advance.

var result=table.GroupBy(c => c.email)
.Select(group => new {
Email = group.Key,
Domain_Count = group.Count()
}).OrderByDescending(x=> x.Domain_Count);
bind to GridView:
yourgidView.DataSource = result.ToList();

Related

Join with last record of details table

Please consider these two tables in my database:
Header:
Id Name
-------------------------------
1 London
2 Berlin
3 Paris
and Details:
Id HeaderId Amount YearMonth
--------------------------------------------------------------------
1 1 1000 2010-01
2 1 2000 2010-05
3 2 3000 2015-04
4 2 2700 2017-12
5 2 4500 2016-10
6 2 7000 2011-09
7 1 3000 2009-05
I want Header records with related Last Details record. For example:
HeaderId HeaderName Amount
----------------------------------------------------
1 London 2000
2 Berlin 2700
3 Paris Null
I wrote this query for Inner Join version (But I want Outer Join version):
from h in Header
join d in Details
on h.Id equals d.HeaderId
select new
{
HeaderId = h.Id,
HeaderName = h.Name,
Amount = (Details.Where(k=>k.HeaderId == h.Id).OrderBy(m=>m.YearMonth).LastOrDefault() == null ? null : Details.Where(k=>k.HeaderId == h.Id).OrderBy(m=>m.YearMonth).LastOrDefault().Amount,
}
and I got this error:
System.NotSupportedException: LINQ to Entities does not recognize the method 'Details.LastOrDefault()Details' method, and this method cannot be translated into a store expression.
How can I get above result?
thanks
This query should return desired result:
from h in Header
from d in Details.Where(d => d.HeaderId == h.Id)
.OrderByDescending(d => d.YearMonth)
.Take(1)
.DefaultIfEmpty()
select new
{
HeaderId = h.Id,
HeaderName = h.Name,
Amount = d.Amount
}
You should change your code as :
Amount = Details.Where(k=>k.HeaderId == h.Id).OrderByDescending(m => m.YearMonth).FirstOrDefault(o=>o.Amount);

LINQ Query multiple orderby of joined tables

I have following LinQ query
var CGTABLE = (from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C,
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ThenBy(g => g.C.OrderBy(c => c.CCode)).ToList();
which runs fine, but it is not doing second orderby using ThenBy ThenBy(g => g.C.OrderBy(c => c.CCode) What am I missing?
Sample data for better understanding.
Data in Tables
2
1
2
4
3
1
4
5
2
1
3
3
1
Should output after both outer and inner list ordered by
1
1
2
3
4
2
1
2
4
5
3
1
3
But Currently it is showing
1
4
5
2
1
2
1
2
4
3
3
3
1
You didn't want to order the main list, you are looking for a way to order inner list inside of outer one, I think.
So below code will do it for you:
var CGTABLE = (
from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C.OrderBy(x => x.CCode),
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ToList();

entity linq for top 100 complicated query

i have the following structure in a table:
UniqueId Type size RelatedId
--------------------------------------------
1 honda 100 1
2 mercedes 200 3
3 mazda 300 4
4 mercedes 150 3
5 merecedes 200 3
6 ford 180 2
7 mercedes 900 5
and so on ....
I want to write a query to get :
The Top 3 items grouped by ONLY honda or mercedes ordered by their size.
Also, when considering rows for a particular Type, there could be multiple rows which may have the same RelatedId, if thats the case, i want to consider the row which is the max UniqueId for that specific RelatedId.
For eg: in the table below, i have 3 rows with RelatedId = 3 for mercedes type,
2 mercedes 200 3
4 mercedes 150 3
5 mercedes 200 3
In this case, I want to consider only the row with UniqueId = 5 as that is the latest.
In my result set for group type : "mercedes", i would expect my result set to get :
mercedes :
7 mercedes 900 5
5 merecedes 200 3
I was able to write something like this:
items.GroupBy( x => x.Type).SelectMany(g => g.OrderByDescending(r => r.size)).Take(10).
How do i group by so that ONLY honda or mercedes are considered?
Also, this includes rows which could have the same RelatedId...(i want the row which has the highest UniqueId in that case for these rows) how do i change the query to account for this?
My expected final result set:
1 honda 100 1
7 mercedes 900 5
5 merecedes 200 3
thanks everyone.
Well the first thing is add a Where to your query to get only Honda and Mercedes
items.Where(e=>e.Type=="honda" || e.Type=="mercedes")...
Now for the second part you can group by multiple columns:
items.Where(e=>e.Type=="honda" || e.Type=="mercedes")
.GroupBy( x => new{x.Type,x.RelatedId})
.Select(g => g.OrderByDescending(r => r.UniqueId).FirstOrDefault())
.OrderByDescending(r => r.size)
.Take(10);
How do i group by so that ONLY honda or mercedes are considered?
items.Where(x => x.Type == "honda" || x.Type == "mercedes").GroupBy(etc...)

EF6 - Return calculated grouped results within a single query

I have a usage table which stores daily usage for customers of various products. I want to now return a result which groups results by customerID for total / combined usage of various products.
See below a perfectly illustrated example of the current data structure :)
id | customerID | prod1 | prod2
1 . 123 . 0 . 1
2 . 125 . 5 . 5
3 . 125 . 1 . 1
I am looking to return a result set as such (again, admire my illustrating ability):
customerID | prod1 | prod2
123 . 0 . 1
125 . 6 . 6
Will this kind of calculation be possible using EF? I am trying to avoid a multitude of loops to achieve the same thing so this would greatly help a brother out.
What you need is GroupBy and Sum:
var result = context.Customers
// .Where(filter) // filter if needed
.GroupBy(m => m.CustomerID)
.Select(g => new
{
CustomerID = g.Key, // We grouped according to CustomerID, so key = CustomerID
SumProd1 = g.Sum(m => m.Prod1), // Sum Prod1 of grouped data
SumProd2 = g.Sum(m => m.Prod2) // Sum Prod2 of grouped data
})
.ToList();
Note: ToList() is for retrieving data, it is not needed if you plan to work on query.

Recursive get all childrens using LINQ C#

i want to show all the parentID as possible using linq for example:
if i select "submodule_idparent = 6" return all 5 and return 1. Because, to show 6 i need 5 and 1.
submodule_id, submodule_name, submodule_idparent
1 Articles null
2 Suppliers null
3 Adjustment 1
4 Presentations 1
5 Categories 1
6 Subcategories 5
7 Corridors 1
8 Cellars 1
9 Purchases 2
I'm try some like that with the next code (MySQL)
SELECT DISTINCT(submodule_id) FROM users_privileges LEFT JOIN modules_options USING(moduleoption_id) WHERE users_privileges.user_id = 1
but is not recursive.
Thanks in advance (y).
Try this:
var query=GetAll(6);
public IEnumerable<users_privileges> GetAll(int submodule_id)
{
var query = from c in db.users_privileges
where c.submodule_id == submodule_id
select c;
return query.ToList().Concat(query.ToList().SelectMany(t => GetAll(t.submodule_idparent)));
}
http://blog.csdn.net/q107770540/article/details/7708418

Categories

Resources