having and conditional count() in linq query - linq

I want to create this query:
select Something, count(Something) as "Num_Of_Times"
from tbl_results
group by Something
having count(Something)>5
I started with this:
tempResults.GroupBy(dataRow => dataRow.Field<string>("Something"))
.Count() //(.......what comes here , to make Count()>5?)

from item in tbl_results
group item by item.Something into groupedItems
let count = groupedItems.Count()
where count > 5
select new { Something = groupedItems.Key, Num_Of_Times = count };
UPDATE : This would give you the result as an IQueryable<DataRow> :
DataTable dt= new DataTable();
dt.Columns.Add("Something", typeof(int));
dt.Columns.Add("Num_Of_Times", typeof(int));
var results = (from item in tbl_results
group item by item.Something into groupedItems
let count = groupedItems.Count()
where count > 2
select dt.Rows.Add(groupedItems.Key, count)).AsQueryable();
(note that it also fills the dt table)

Related

Reference DataTable columns with Linq

I am trying to join two datatables using linq
var invoices420 = dt420_.AsEnumerable();
var invoices430 = dt430_.AsEnumerable();
var query = from inv430 in invoices430
join inv420 in invoices420 on inv430.LinkDoc equals inv420.LinkDoc
orderby inv430.SID
select new
{
LinkDoc = inv430.LinkDoc,
TotalIn = Math.Round(inv430.Credit, 2),
TotalOut = ((inv420 == null) ? 0 : Math.Round(inv420.Debit, 2))
};
Joining does not seems to be a problem, but I am getting an error'System.Data.DataRow' does not contain a definition for 'LinkDoc' and no extension method 'LinkDoc' accepting a first argument of type 'System.Data.DataRow' could be found (are you missing a using directive or an assembly reference?).
What do I have to do to reference a column in DataTable for example inv430.LinkDoc without using inv430.Field("linkdoc")?
If I want to do a group by on result set I am thinking
var q2 = query
.GroupBy(item => item.LinkDoc);
return q2.ToArray();
Problem is that in q2 I dont get all the columns (linkdoc, totalin, totalout).
Original data is
dt420_
Linkdoc Credit
Invoice1 500
Invoice2 100
Invoice3 200
dt430_
LinkDoc Debit
Invoice1 100
Invoice1 100
Invoice2 200
Result would be
LinkDoc TotalIn(Credit) TotalOut(Debit)
Invoice1 500 200
Invoice2 100 200
Invoice3 200 0
You need to replace all places you called directly to properties like
inv430.LinkDoc
to
inv430["LinkDoc"]
inv430 is a DataRow so you need to use the indexer that gets a string.
EDIT:
Your join will bring wrong data (see my comment below). You need to use this code:
var group430 = from inv430 in invoices430
group inv430 by inv430["LinkDoc"].ToString().Trim() into g
select new
{
LinkDoc = g.Key.ToString().Trim(),
TotalOut = g.Sum(inv => Math.Round((decimal)inv["Debit"], 2))
};
var group420 = from inv420 in invoices420
group inv420 by inv420["LinkDoc"].ToString().Trim() into g
select new
{
LinkDoc = g.Key.ToString().Trim(),
TotalIn = g.Sum(inv => Math.Round((decimal)inv["Credit"], 2))
};
var result = from inv430 in group430
join inv420 in group420 on inv430.LinkDoc equals inv420.LinkDoc into inv
from inv420 in inv.DefaultIfEmpty()
select new
{
inv430.LinkDoc,
TotalOut = inv430.TotalOut,
TotalIn = inv420 != null ? inv420.TotalIn : 0
};

Linq to Sql Query - better solution (optimizing)

The following code works, but it's not a nice code. (low performance)
I have a dictionary with value and key.
First i go trough every webcodes who exist. Then i load all participants in a list (where webcode equals the actual webcode in the foreach). After that i add the data (parameter of the webcode and a count of participants to the dictionary).
Guid compID = Guid.Parse(wID);
ChartModel webcodes = new ChartModel();
webcodes.Title = "Webcodes Statistics";
webcodes.Data = new Dictionary<string, int>();
var webcodesData = db.t_Webcode;
foreach (var w in webcodesData)
{
var wData = db.t_Participant.Where(t => t.FK_Competition == compID && t.Webcode == w.Webcode);
if (wData.Count() != 0)
webcodes.Data.Add(w.Parameter, wData.Count());
}
ViewBag.Webcodes = webcodes;
TIA
You need something along these lines:
webcodes.Data = (from w in db.t_Webcode
join p in db.t_Participant on w.Webcode equals p.Webcode
where p.FK_Competition == compID
group w by w.Parameter into g
select new { g.Key, Count = g.Count() }).ToDictionary();
I can't test it but that is the type of query you need.
This will assume that you have relationships defined in your database and that your LINQ to SQL datacontext are aware of them. If not, you will need to join manually on t_Participants from tWebcode.
This should execute in 1 single SQL query, instead of 1 query per row in tWebcode.
var webcodesAndNoOfParticipants =
from webcode in db.tWebcode
// Define number of participants for this webcode
let numberOfParticipants = webcode.t_Participants.Count(participant => participant.FK_Competition == compID)
where numberOfParticipants > 0
select new {
WebcodeParameter = webcode.Parameter,
NoOfParticipants = numberOfParticipants
};
webcodes.Data = webcodesAndNoOfParticipants.ToDictionary(x => x.WebcodeParameter, x => x.NoOfParticipants);

Linq group by month

I have a table with a date field and time field.I want to retrieve a result set grouped by the month and the number of records that appear within that month. How can this be done in LINQ?
Something like this.
Data.
10/10/2011 00:00:10:000
10/11/2011 00:00:20:000
11/01/2011 00:00:10:000
11/10/2011 00:00:40:000
I want to retrieve
10/1/2011 00:00:30:000
11/1/2011 00:00:50:000
Thank`s for help.
i am try something like this.
var monthely =
from u in sqlDataContract.TimeTrickTables
group u by u.EntryDate.Value.Month into g
select new
{
EntryDate = g.Key,
ETime = g.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add((TimeSpan)t.ETime))
};
but it throw this Exception The query operator 'Aggregate' is not supported
from u in TimeTrickTables
group u by u.EntryDate.Value.Month into g
select new
{
Month = g.Key,
Count = g.Count(), //Nr. of records
Time = new TimeSpan(g.Sum(x => x.ETime.Ticks)) //Total time
}

Using LINQ to get all the records in a table having the same value in a specific column

I am trying to get all the rows in a table having the same value in a column. I got it working by using group by:
var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
group row by row.Field<string>("A");
foreach (var group in groupedData)
{
if (group.Count() > 1)
{
//select from each group only the DataRows
//having a certain value in a second column
foreach (var dataRow in group)
{
multipleRowsList.Add(dataRow);
}
}
}
I would like to avoid calling foreach ,get only the groups having a count > 1 and then get ONLY
the DataRows that have a second column with a specific value. Thanks!
try this:
var query = from row in excelDataSource
group row by row.Field<string>("A") into g
select new { Value = g.Key, Rows = g };
var nonZeroRows= from q in query
where q.Rows.Count() > 0
select q.Rows;
// at this point you have an enumerable of enumerables of tablerows.
var list = nonZeroRows.Aggregate(Enumerable.Empty<TableRow>(),
(a, b) => a.Concat(b.Where(c => c.Something == true)); // your condition here
Thanks Atanamir! Here is the final code, just wonder if you have any better ways of doing it. the end goal of this is to flag one of the rows that is entered twice.
var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
group row by row.Field<string>("A")
into g
select new {Value = g.Key, Rows = g};
var nonZeroesRows = from q in groupedData
where q.Rows.Count() > 1
select q.Rows;
//at this point you have an enumerable of enumerables of tables rows
var listRows = nonZeroesRows.Aggregate(Enumerable.Empty<DataRow>(),
(a, b) => a.Concat(b.Where(c => c.Field<bool>("Omit Row") == false)));
//grouped them again and get only the last row from the group wiht a count > 1
var doubleRows = from row in listRows
group row by row.Field<string>("A")
into g
where g.Count() > 1
select g.Last();
Or maybe better:
var groupedData = from row in Tab1Model.ExcelGridDataSource.AsEnumerable()
group row by row.Field<string>("A")
into g
where g.Count() > 1
select new {/*Value = g.Key,*/ Rows = g};
//at this point you have an enumerable of enumerables of tables rows
var listRows = groupedData.Aggregate(Enumerable.Empty<DataRow>(),
(a, b) => a.Concat(b.Rows.Where(c => c.Field<bool>("Omit Row") == false)));
//grouped them again and get only the last row from the group wiht a count > 1
var doubleRows = from row in listRows
group row by row.Field<string>("A")
into g
where g.Count() > 1
select g.Last();

Linq extract a count() value from a data object

I have divAssignments that has potential multiple rows by rNI, an official id, according to a compound key of Indictment and booking numbers.
rNI Booking Indictment
12345 954445 10 12345
12345 954445 10 12346
12345 954445 10 12347
So ID has a count of 3 for a single booking number for this rni.
I get lost attempting to generate a count and a group by booking Number:
var moreThen = from dA in divAssignments
select new { dA.rNI, IndictmentCount = dA.indictmentNumber.Count() };
Most of the examples are dealing with static int[] and don't seem to work in my case.
How do I get a group and then a count? If I could put in a having that would be fantastic.
from a t-sql POV I'd use this:
Select rni, bookingNumber, count(*) IndictmentCount
from divAssignments
group by rni, bookingNumber
having count(*) > 0
TIA
How about something like this:
var query = from item in divAssignments
group item by item.rNI into grouping
select new
{
Id = grouping.Key,
Count = grouping.Count()
}
If you're interested in grouping by both the rNI and the booking number, I would change it to this:
var query = from item in divAssignements
group item by new { item.rNI, a.Booking } into grouping
select new
{
Id = grouping.Key,
Count = grouping.Count
};
OR
var query = from item in divAssignments
group item by item into grouping
select new
{
Id = grouping.Key,
Count = grouping.Count()
}
and implement IEquatable on the divAssignment object to support equality comparison. The other option if you'd like is to write an IEqualityComparer instance to do the composite key comparison. Your query could then look like:
var query =
divAssignments
.GroupBy(i => i, new MyCustomEqualityComparer())
.Select(i => new { Key = i.Key, Count = i.Count());
var query =
from dA in divAssignments
group dA by new { dA.rNI, dA.bookingNumber };
foreach(var grp in query)
{
Console.WriteLine("rNI={0}, bookingNumber={1} => Count={2}", grp.Key.rNI, grp.Key.bookingNumber, grp.Count());
}
If you use a Grouping operator in Linq you will get what you need. The code:
var count = from a in divAssignments
group a by new { a.rNI, a.Booking } into b
select b;
will return a collection of IGrouping objects. This will give you the Key (in my example this will be an anonymous type with an rNI and a Booking property) and a collection of the divAssignments that match the key.
Using Method syntax (much easier to read in my opinion):
First group the records, then select a new result for each group that contains the count.
var groups = divAssignments.GroupBy(d => new { d.rNI, d.Booking });
groups.Select(g=> new { g.Key.rNI, g.Key.Booking, IndictmentCount = g.Count() });

Resources