Getting Rounded Values in the final result of Linq Query - c#

How can the below linq Query be modified such that i can get a Rounded figure of values.
var result=GetStudentsWithTheirMarks()
.OrderByDescending(x => Math.Round(x.PercentageScore)).Take(5)
.OrderBy(x => x.PercentageScore);
Please ignore the presence of two order by clause as this is done for with a purpose.
GetStudentsWithThierMarks returns Student List with their FirstName and PercentageScore.
I believe in the above query Math.Round is only applicable when during order by operation so final result still contains values in decimal places whereas i am only interested to see rounded figures with integer values. I just cant figure out the syntax.

You just need a Select :
var result= GetStudentsWithTheirMarks()
.OrderByDescending(x => Math.Round(x.PercentageScore))
.Take(5)
.OrderBy(x => x.PercentageScore)
.Select(x => Math.Round(x.PercentageScore));

You can store this value in an anonymous type:
var result = GetStudentsWithTheirMarks()
.Select(s => new
{
Student = s,
RoundedPercentageScore = Math.Round(s.PercentageScore)
})
.OrderByDescending(x => x.RoundedPercentageScore )
.Take(5)
.OrderBy(x => x.Student.PercentageScore);
Now you can access it in this way:
foreach(var x in result)
Console.WriteLine("RoundedPercentageScore: " x.RoundedPercentageScore);

Related

LINQ groupby search in contains

I have the following result:
var result = (from p1 in db.Table
select new ReportInform
{
DataValue = p1.DataValue,
SampleDate = p1.SampleDate
})
.Distinct()
.ToList();
// Next getting list of duplicate SampleDates
var duplicates = result.GroupBy(x => x.SampleDate)
.Where(g => g.Count() > 1)
.Select (x => x)
.ToList();
foreach (var r in result)
{
if (duplicates.Contains(r.SampleDate)) // get error here on incompatbility
{
r.SampleDate = r.SampleDate.Value.AddMilliseconds(index++);
}
}
Cannot convert from 'System.DateTime?' to 'System.Linq.IGrouping
That error is pretty clear but may not be at a first glance. As a programmer, you need to learn how to read, understand and make sense of compiler or runtime errors.
Anyhow it is complaining that it cannot convert DateTime? to System.Linq.IGrouping<System.DateTime, ReportInForm>. Why? Because this query returns an System.Linq.IGrouping<System.DateTime, ReportInForm>
var duplicates = result.GroupBy(x => x.SampleDate)
.Where(g => g.Count() > 1)
.Select (x => x)
.ToList();
The GroupBy method returns IGrouping<System.DateTime, ReportInForm> which has a Key and the Key is the thing you grouped by and a list of items in that group. You are grouping by SampleDate and checking if there are more than one items in that group and then selecting the group. Thus dulplicates has a list of IGrouping<System.DateTime, ReportInForm> and you are asking the runtime to check if it contains a DateTime? and it blows up at this line:
duplicates.Contains(r.SampleDate)
One way to fix this is: What you want to do is to select the key of that group. Thus do this:
.Select (x => x.Key)
If you are expecting duplicates to be of type List<DateTime?> then you meant to write this
.Select(x => x.Key)
instead of
.Select(x => x)

orderByDescending date, month and year

I am using a LINQ query to get the data order by descending date. but when the year is changed then descending order of date is not in the proper way
Here is my code
In this code, LastLogin is a column with DateTime datatype. I have to sort data by this column name.
var res = objUI.Where(x => x.LastLogin != null)
.GroupBy(x => x.LastLogin.Value.Date)
.Select(x => new { LastLogin = string.Format("{0:MM/dd/yyyy}", x.Key) })
.OrderByDescending(x => x.LastLogin)
.ToList();
You shouldn't to try order the date as string;
var res = objUI.Where(x => x.LastLogin != null)
.GroupBy(x => x.LastLogin.Value.Date)
.OrderByDescending(x => x.Key)
.Select(x => new { LastLogin = string.Format("{0:MM/dd/yyyy}", x.Key) })
.ToList();
You're ordering by your string representation which starts with the month.
It looks like you're actually only interested in distinct dates though - grouping and then discarding everything other than the key is equivalent to just projecting and then taking distinct data. I'd also suggest avoiding an anonymous type unless you really need it - an anonymous type with only a single property is usually a bad idea.
Finally, if you're formatting a single value - certainly a DateTime value - it's simpler to just call the ToString method. In the code below I've explicitly specified the invariant culture too - otherwise you may find an unexpected calendar system is used...
So I'd write:
var res = objUI.Where(x => x.LastLogin != null)
.Select(x => x.LastLogin)
.Distinct()
.OrderByDescending(x => x)
.Select(x => x.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture))
.ToList();

LINQ - How to get subset of columns after GroupBy

This LINQ-to-SQL query works (testing in LINQpad):
var q5 = LOGs.Where(r => r.APP_NAME == "Toaster")
.GroupBy(pol => pol.CASE_NO)
.Select(grp => grp.First())
.OrderByDescending(l => l.WHEN);
q5.Dump();
However, that returns all columns for each row.
How can I refine the Select() part to specify certain columns?
I can do it in two steps by adding .ToList() to the query, then querying q5:
var q5a = q5.Select(r => new {CASE=r.CASE_NO, WHEN = r.WHEN});
q5a.Dump();
Can I accomplish that in one statement instead of two?
Thanks --
why don't you filter after where?
var q5 = LOGs.Where(r => r.APP_NAME == "Toaster")
.Select(r=> new{r.CASE_NO, r.WHEN})
.GroupBy(pol => pol.CASE_NO)
.Select(grp => grp.First())
.OrderByDescending(l => l.WHEN);
remembar that new {CASE=r.CASE_NO, WHEN = r.WHEN} creates a new anonymous type because of differents property names, new {r.CASE_NO, r.WHEN} doesn't !

Multiple GroupBy in 1 linq expression

I cannot seem to combine 2 GroupBy statements in 1 linq expression..
For now i'm doing something like this:
double maxInvestment = 0;
foreach (var playerAction in TotalUserActions.GroupBy(p => p.Player))
{
var MaxInvestmentPerPlayer = playerAction.GroupBy(p => p.RoundId)
.Select(p => p.LastOrDefault())
.Sum(p=> p.BetSize);
if(MaxInvestmentPerPlayer > maxInvestment)
maxInvestment = MaxInvestmentPerPlayer;
}
What I would like to do is something like this...
double maxInvestment = TotalUserActions.GroupBy(p => p.Player)
.GroupBy(p => p.RoundId)
.Select(p => p.LastOrDefault())
.Sum(p=> p.BetSize);
But that wont work.. Can someone help me on this?
Thanks!
Looks like this is what you want, the key takeaway being the inner query is wrapped in an outer call to Select():
var maxInvestment = TotalUserActions.GroupBy(p => p.Player)
.Select(g => g.GroupBy(x => x.RoundId)
.Select(x => x.LastOrDefault())
.Sum(x => x.BetSize))
.Max();
I do question your use of LastOrDefault() though as, since you have not specified any ordering, you may as well use FirstOrDefault() and save the hassle of skipping to the last element.

Counting grouped data with Linq to Sql

I have a database of documents in an array, each with an owner and a document type, and I'm trying to get a list of the 5 most common document types for a specific user.
var docTypes = _documentRepository.GetAll()
.Where(x => x.Owner.Id == LoggedInUser.Id)
.GroupBy(x => x.DocumentType.Id);
This returns all the documents belonging to a specific owner and grouped as I need them, I now need a way to extract the ids of the most common document types. I'm not too familiar with Linq to Sql, so any help would be great.
This would order the groups by count descending and then take the top 5 of them, you could adapt to another number or completely take out the Take() if its not needed in your case:
var mostCommon = docTypes.OrderByDescending( x => x.Count()).Take(5);
To just select the top document keys:
var mostCommonDocTypes = docTypes.OrderByDescending( x => x.Count())
.Select( x=> x.Key)
.Take(5);
You can also of course combine this with your original query by appending/chaining it, just separated for clarity in this answer.
Using the Select you can get the value from the Key of the Grouping (the Id) and then a count of each item in the grouping.
var docTypes = _documentRepository.GetAll()
.Where(x => x.Owner.Id == LoggedInUser.Id)
.GroupBy(x => x.DocumentType.Id)
.Select(groupingById=>
new
{
Id = groupingById.Key,
Count = groupingById.Count(),
})
.OrderByDescending(x => x.Count);

Categories