Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone please suggest how to get the sum of points from all the Event objects by using linq.
new Category
{
CategoryId = "NUTR",
CategoryName = "NUTR",
CategoryDescription = "NUTR Desc",
TotalPoints = "100",
Events = new List<Event>
{
new Event{Firstname = "HELEN",Surname = "BECKETT",
Points = "10", Description = "NUTR Desc",Eventdate = "2013/04/19",EntityNumber = "1203206956"},
new Event{Firstname = "PAUL",Surname = "BECKETT",
Points = "90", Description = "NUTR Desc",Eventdate = "2013/06/19",EntityNumber = "1203206957"}
}
}
You need to use SelectMany to flatten the list of events, then do the sum:
var totalPoints = categories
.SelectMany(c => c.Events)
.Sum(e => Convert.ToInt32(e.Points));
If I correclty understand the question this can be achieved by Linq
I suggest you to change the type of Points from string to int if possible
Category cat = ....
var countEvents = cat.Sum(i=>i.Events.Sum(ii=>ii.Points));
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a list with tickets in it:
TicketID - Aantal (amount) - ActiePrijs (price)
For all those tickets the ActiePrijs (price) is still empty. But I also have a list with only those Actieprijs (prices). It's just a list of decimals.
Now I want to put the first actiePrijs from the decimal list into the ActiePrijs from the first ticket, the second ActiePrijs into the ActiePrijs of the second ticket etc.
I want to do it using linq method syntax.
Can someone help me?
I assume tickets & price lists are tickets and price.
You can use linq like below.
tickets = tickets.Select((ticket, index) => new Ticket
{
TicketID = ticket.TicketID,
Aantal = ticket.Aantal,
ActiePrijs = price[index]
})
.ToList();
Or if you have more properties and you do not want to create new object then use like below.
tickets = tickets.Select((ticket, index) =>
{
ticket.ActiePrijs = price[index];
return ticket;
})
.ToList();
You dont show any code, but I thing you want something like this.
public class Properties
{
public int Aantal { get; set; }
public int Price { get; set; }
}
List<Properties> l = new List<Properties>();
l.Add(new Properties() { Aantal = 1 });
l.Add(new Properties() { Aantal = 2 });
l.Add(new Properties() { Aantal = 3 });
List<int> l2 = new List<int>();
l2.Add(1);
l2.Add(2);
l2.Add(3);
int index = 0;
l.ForEach(x => x.Price = l2[index++]);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have the following code in MySQL
SELECT Query,SUM(TotalResults)
FROM TotalJobResults
GROUP BY Query
ORDER BY `SUM(TotalResults)` DESC
I am trying to duplicate this function calling a MongoDB Collection in C# and cannot seem to actually understand it very well. I have the code below but it seems far from answering the problem,
var t = from q in _jobCollection.Find(_ => true).ToList().GroupBy(
p => p.Query,
p => p.TotalResults,
(key, g) => new { Query = key, Total = g.ToList().Sum() }
);
Thanks for any light you can shed on this!
Looks like it works if I put a select q on the end. hmm! my error
var t = from q in _jobCollection.Find(_ => true).ToList().GroupBy(
p => p.Query,
p => p.TotalResults,
(key, g) => new { Query = key, Total = g.ToList().Sum() }
) select q;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an existing List that countains data
List<string>countries
I want to somehow loop through this data an populate the following collection
IEnumerable<SelectListItem> Countries
How would I go about in doing this?
The simplest method I can think off is using a Linq query:
Countries = countries.Select(country => new SelectListItem {Text = country, Value = country});
You won't have the ability to differ the text from the value, however.
Presuming this is related to mvc... try this
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem { Text = c, Value = c });
Something like this I think.
IEnumerable<SelectListItem> Countries = countries.Select(c => new SelectListItem
{
Text = c,
Value = c
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if(some condition)
{
var columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
}).ToList();
}
}
How can i make this variable columnSeries a global variable? i have searched a lot on this, found list dynamic> new{}; but none of them are working so help will be really appreciated
Make the anonymous type you want a class.
public class ColumnSeries
{
public string type {get; set;}
//...
}
//class level variable
IEnumerable<ColumnSeries> columnSeries = null;
//then create the ColumnSeries list
columnSeries = (from l in logs
group l by l.MonitoringPorfileName into grp
select new ColumnSeries
{
type = "column",
name = grp.Key,
data = (from h in Hours
let gd = grp.Where(x => x.Hours == h)
select gd.Sum(x => x.Count)).ToArray()
});
You are creating an anonymous type inside your if statment but you want to use the result ouside the scope of the if statement. Usually you just define 'columnSeries' before the if BUT this is an anonymous type so it's not obvous.
So, before the if statement do the following (untested but should be close):
var columnSeries = Enumerable.Repeat(new {type="", name="", data=new int[]{0}}, 0);
Check out this question for more info