Need help writing nested SQL query in C# - c#

I need some help with building an SQL query. Here's the scenario.
In this only table I have, there are over 22 columns. For now I'm only focusing on 3:
---------------------------------------------------------
| id | Status | EditNumber |
---------------------------------------------------------
| A0001 | Approved | AG01 |
| A0002 | Approved | AG02 |
| A0003 | Approved | AG01 |
| A0004 | Approved | AG03 |
| A0005 | Other | AG01 |
| A0006 | Other | AG02 |
| A0007 | Pending | AG01 |
| A0008 | Pending | AG03 |
| A0009 | Denied | AG04 |
| A0010 | Denied | AG03 |
| A0011 | Approved | AG02 |
| A0012 | Approved | AG01 |
| A0013 | Approved | AG03 |
| A0014 | Denied | AG01 |
| A0015 | Pending | AG04 |
| A0016 | Pending | AG01 |
---------------------------------------------------------
This is just an example. Now, what I need is the count of each EditNumber and the count of each status for each EditNumber
This is what I have done.
var subquery = from n in db.Claims
group n by new { n.id, EditNumber = n.EditNumber.Substring(0, 4) } into g
select new
{
Key = g.Key,
Count = g.Count()
};
var count = (from c in db.Claims
join s in subquery on c.ClaimID equals s.Key.ClaimID
group s by s.Key.EditNumber into g
join e in db.EditOverrides on g.Key equals e.EditNumber
orderby g.Count() descending
select new EOSummaryCount
{
EditNumber = g.Key,
Count = g.Count(),
Description = e.Description
}).AsEnumerable();
but this only gives me the count of EditNumber.
and the following:
db.C.Where(w => w.RecordType == type)
.GroupBy(x => x.Status)
.Select(s => new StatusCount { Status = s.Key, Count = s.Count() });
only gives me the status count. But only gives me in format i.e. in JSON I see the result as
{
"key":"PENDING",
"count":93
},{
"key":"CLOSED",
"count":128
},{
"key":"MEDREVIEW",
"count":218
},{
"key":"APPROVED",
"count":3946
},{
"key":"DENIED",
"count":746
}
What I ultimately want in JSON format is something like this:
[{
"editNumber":"AG001",
"count":2195,
"status":[
{
"key":"INPROCESS",
"count":3
},{
"key":"PENDING",
"count":93
},{
"key":"CLOSED",
"count":128
},{
"key":"MEDREVIEW",
"count":218
},{
"key":"APPROVED",
"count":3946
},{
"key":"DENIED",
"count":746
}]
},{
"editNumber":"AG002",
"count":234,
"status":[
{
"key":"INPROCESS",
"count":3
},{
"key":"PENDING",
"count":93
},{
"key":"CLOSED",
"count":0
},{
"key":"MEDREVIEW",
"count":218
},{
"key":"APPROVED",
"count":104
},{
"key":"DENIED",
"count":30
}]
}]
How can I get this result? I couldn't figure out how I could join those two together to get a new result with the above JSON. Btw, it's all in WebApi controller
that returns HttpResponseMessage.

Without seeing your table structure, you could try this:
var result = db.Claims
.GroupBy(c => c.EditNumber)
.Select(g => new {
editNumber = g.FirstOrDefault().EditNumber,
count = g.Count(),
status = g.GroupBy(x => x.Status).Select(sg => new {
key = sg.FirstOrDefault().Status,
count = sg.Count(),
})
}).ToList();

You can do it as below
from p in Samples
group p.Status by p.EditNumber into g
select new { EditNumber = g.Key, EditCount= g.Count(),
StatusList = from x in g.ToList() group x by x into xc select new { Status = xc.Key, StatusCount= xc.Count() } }

Related

Count and Max Columns Group By in LINQ

I have model
public class Rate
{
public int Nr{ get; set; }
public string Rate{ get; set; }
public int Order{ get; set; }
}
and a RateList = List<Rate> like this
Nr | Rate | Order
123 | A | 2
425 | A+ | 1
454 | B | 4
656 | B+ | 3
465 | A | 2
765 | B | 4
Notice that Order always match the Rate (A+ = 1, A = 2, B+ = 3, B = 4, C+ = 5 ...)
I want to count how many time the Rate occoured and display order by the Order
The result should look like this
Rate | Count | Order
A+ | 1 | 1
A | 2 | 2
B+ | 1 | 3
B | 2 | 4
or without column Order
Rate | Count
A+ | 1
A | 2
B+ | 1
B | 2
In SQL I could do like this if I had above list in table Tab
SELECT Rate, COUNT(Rate), Max(Order) from Tab group by Rate
but in LINQ?
I was trying something like this
var rating= RateList.Distinct().GroupBy(x => x.Rate)
.Select(x => new { Rate = x.Key, RateCount = x.Count() })
.OrderBy(x => x.Order);
but didnt work.
Thank You for help.
Your SQL query is equevalent to:
var rating = rateList.GroupBy(x => x.Rate)
.Select(x => new {
Rate = x.Key,
RateCount = x.Count(e => e != null),
Max = x.Max(g => g.Order)
});

Converting SQL to Linq query

I'm trying to get the output of the following query into a Linq query
SELECT SearchQueries.Query,
Clicks.Name,
COUNT (SearchQueries.Query) AS Hits
FROM SearchQueries
INNER JOIN Clicks ON Clicks.SearchqueryId = SearchQueries.Id
GROUP BY SearchQueries.Query, Clicks.Name
ORDER BY Hits DESC
But I can't seem to figure out how to do this;
this is what I have so far
var result =
_db.Clicks.Select(q => q)
.GroupBy(q => q.Name, g => g.Searchquery.Query)
.ToDictionary(g=> g.Key, g => g);
but how would I continue?
the result is something like this:
+---------------+-------------------+------+
|Query | Name | Hits |
+---------------+-------------------+------+
|tag | dnfsklmfnsd | 53 |
|tag2 | dgsqfsdf | 17 |
+---------------+-------------------+------+
The original tables looks like following
SearchQueries;
+---+-------+
|Id | Query |
+---+-------+
| 1 | tag | x 53
| 2 | tag2 | x 17
+---+-------+
Clicks;
+---+-------------------+---------------+
|Id | Name | SearchqueryId |
+---+-------------------+---------------+
| 1 | dnfsklmfnsd | 1 |
| 2 | dgsqfsdf | 2 |
+---+-------------------+---------------+
Try to use GroupBy and Count: (I changed the order to using SearchQueries as "base table" in the expression, just to make it more easy to compare to the SQL-statement)
var result =
_db.SearchQueries
.GroupBy(sq => new { name = sq.Clicks.Name, query = sq.Query)
.Select(sq => new {
Query = sq.Query,
Name = sq.Clicks.Name,
Hits = sq.Count()
})
.OrderByDescending(sq => sq.Hits);
Well, if you have a navigation property Searchquery on Click, as it looks like, you can do
var result =
_db.Clicks
.GroupBy(m => new {name = m.Name, query = m.Searchquery.Query)
.Select(g => new {
Query = g.Key.query,
Name = g.Key.name,
Hits = g.Count()
});

Select all columns but group by only one in linq

I have been looking for a way to get multiple columns but group by only one in SQL and I found some info. However I can not came up with a way to do it in linq.
I have the following toy example table:
| Id | Message | GroupId | Date |
|-------------------------------|
| 1 | Hello | 1 | 1:00 |
| 2 | Hello | 1 | 1:01 |
| 3 | Hey | 2 | 2:00 |
| 4 | Dude | 3 | 3:00 |
| 5 | Dude | 3 | 3:01 |
And I would like to recover all columns for the rows that have a distinct GroupId as follows (with a 'Date' desc order):
| Id | Message | GroupId | Date |
|-------------------------------|
| 1 | Hello | 1 | 1:00 |
| 3 | Hey | 2 | 2:00 |
| 4 | Dude | 3 | 3:00 |
I do not really care about which row is picked from the grouped ones (first, second...) as long as is the only one given that group Id.
I have came out with the following code so far but it does not do what is supposed to:
List<XXX> messages = <MyRep>.Get(<MyWhere>)
.GroupBy(x => x.GroupId)
.Select(grp => grp.OrderBy(x => x.Date))
.OrderBy(y => y.First().Date)
.SelectMany(y => y).ToList();
This will give you one item per group:
List<dynamic> data = new List<dynamic>
{
new {ID = 1, Message = "Hello", GroupId = 1, Date = DateTime.Now},
new {ID = 2, Message = "Hello", GroupId = 1, Date = DateTime.Now},
new {ID = 3, Message = "Hey", GroupId = 2, Date = DateTime.Now},
new {ID = 4, Message = "Dude", GroupId = 3, Date = DateTime.Now},
new {ID = 5, Message = "Dude", GroupId = 3, Date = DateTime.Now},
};
var result = data.GroupBy(item => item.GroupId)
.Select(grouping => grouping.FirstOrDefault())
.OrderByDescending(item => item.Date)
.ToList();
//Or you can also do like this:
var result = data.GroupBy(item => item.GroupId)
.SelectMany(grouping => grouping.Take(1))
.OrderByDescending(item => item.Date)
.ToList();
If you want to control OrderBy then:
var result = data.GroupBy(item => item.GroupId)
.SelectMany(grouping => grouping.OrderBy(item => item.Date).Take(1))
.OrderByDescending(item => item.Date)
.ToList();

linq Group By from DataTable

I have DataTable Like this
Thank you Bob Vale for your help
what is (Select(X,i) mean in your linq,
but as i made a mistake in my table
I have this
No | Size | Type | FB | FP
----------------------------------------
100 | 2 | typeA | FB1 | A1
101 | 3 | typeB | FB1 | A1
101 | 4 | typec | FB1 | A1
103 | 4 | typeC | FB2 | A2
103 | 5 | typeD | FB2 | A2
103 | 6 | typeE | FB2 | A2
I want to have some thing like that
No | Size | Type | FB | FP
---------------------------------
100 | 2 | typeA | FB1 | A1
101 | 3 | typeB | FB1 | A1
| 4 | typec | |
103 | 4 | typeC | FB2 | A2
| 5 | typeD | |
| 6 | typeE | |
How can I make it? I can make Group By
var result = from row in cableDataTable.AsEnumerable()
group row by new
{
FB = row.Field<string>("FB"),
FP = row.Field<string>("FP"),
Size = row.Field<int>("Size"),
Type = row.Field<int>("Type"),
no= row.Field<int>("no"),
} into g
select new
{
FB = g.Key.FB,
FP = g.Key.FP,
Size = g.Key.Size,
Type = g.Key.Type
no= g.Key.no
};
but it that could't give the result
thank you for your attention
How about this:
// First declare a conversion from the DataTable to an anon type
var rows = cableDataTable.AsEnumerable()
.Select(x => new {
Size = x.Field<int>("Size"),
Type= x.Field<string>("Type"),
FB = x.Field<string>("FB"),
FP = x.Field<string>("FP")
});
// Now use group by, ordering and select many to select the rows
var result = rows.GroupBy (row => new {row.FB, row.FP} )
.OrderBy (g => g.Key.FB)
.ThenBy(g => g.Key.FP)
.SelectMany(g => g.OrderBy(row => row.Size)
.Select((x,i) =>
new {
Size = x.Size,
Type = x.Type,
FB = (i==0) ? x.FB : null,
FP= (i==0) ? x.FP : null
}));
You can use linq query as
var result = cableDataTable.AsEnumerable().GroupBy(g => new { g.FB, g.FP}).Select(x => x);

Combining items in a given datatable column using LINQ

I have a datatable which looks like this:
Id | Title | Month | Year |
ebdef240-abb7-4a82-9229-1ed37496da86 | Maths FT | 1 | 2013 |
57504a66-4882-4794-a8b9-af0ead38dc70 | Maths FT | 2 | 2013 |
57504a66-4882-4794-a8b9-af0ead38dc70 | Maths FT | 2 | 2014 |
57504a66-4882-4794-a8b9-af0ead38dc70 | Maths FT | 2 | 2015 |
ebdef239-abb7-4a82-9229-1ed37496da86 | English PT | 1 | 2013 |
ebdef239-abb7-4a82-9229-1ed37496da86 | English PT | 1 | 2014 |
but I would like it to be arranged like this:
Id | Title | Month | Years |
ebdef240-abb7-4a82-9229-1ed37496da86 | Maths FT | 1 | 2013 |
57504a66-4882-4794-a8b9-af0ead38dc70 | Maths FT | 2 | 2013, 2014, 2015 |
ebdef239-abb7-4a82-9229-1ed37496da86 | English PT | 1 | 2013, 2014 |
It maybe that it would make more sense to represent this as a list. I made an attempt at doing this, but am confused as to a) how I can combine the Years (as above, and b) include non-grouped fields, such as the ID (there are others, this is just a few of the columns for simplicity):
From LINQPad:
var objectTable = new DataTable();
objectTable.Columns.Add("Title",typeof(string));
objectTable.Columns.Add("id",typeof(Guid));
objectTable.Columns.Add("Month",typeof(int));
objectTable.Columns.Add("Year",typeof(string));
objectTable.Rows.Add("Maths FT", "ebdef240-abb7-4a82-9229-1ed37496da86", 1, "2013");
objectTable.Rows.Add("Maths FT", "57504a66-4882-4794-a8b9-af0ead38dc70", 2, "2013");
objectTable.Rows.Add("Maths FT", "57504a66-4882-4794-a8b9-af0ead38dc70", 2, "2014");
objectTable.Rows.Add("Maths FT", "57504a66-4882-4794-a8b9-af0ead38dc70", 2, "2015");
objectTable.Rows.Add("English PT", "ebdef239-abb7-4a82-9229-1ed37496da86", 1, "2013");
objectTable.Rows.Add("English PT", "ebdef239-abb7-4a82-9229-1ed37496da86", 1, "2014");
var DataSort = from row in objectTable.AsEnumerable()
group row by new {title = row.Field<string>("Title"), month = row.Field<int>("Month")} into grp
select new
{
Title = grp.Key.title,
Month = grp.Key.month,
};
DataSort.Dump();
Any examples would greatly appreciated.
Thanks.
Perhaps:
var result = objectTable.AsEnumerable()
.Select(r => new { Row = r, Title = r.Field<string>("Title"), Month = r.Field<int>("Month") })
.GroupBy(x => new { x.Title, x.Month })
.Select( g => new {
id = g.First().Row.Field<Guid>("id"),
g.Key.Title,
g.Key.Month,
Year = g.Select(x => x.Row.Field<string>("Year")).ToList()
});
If you want a string with a comma separated list instead of the List<string> for the year-group use Year = string.Join(",", g.Select(x => x.Row.Field<string>("Year"))).
By the way, why is year a string instead of an int?
This will be the LINQ statement for your output
from o in objectTable
group o by new { o.Id, o.Month, o.Title } into g
select new {Id = g.Key.Id, Title = g.Key.Id, Month = g.Key.Month, Years= String.Join(" ", g.Select(x=>x.Year).ToArray()) };

Categories

Resources