SelectedStationCustomers
.SelectMany(customer => String.Format("{0}", customer.CustomerTelephone))
.ToList()
.ForEach(customer => client.SendMessage("12345678", NEED VALUE HERE, textMessage));
How do I get the value from customer.CustomerTelephone, into the ForEach statement? I have tried with customer.ToString(), but that does not seem to work.
You don't want to use SelectMany in this case. Just use Select.
And the String.Format does nothing for you in your example.
SelectedStationCustomers
.Select(customer => customer.CustomerTelephone)
.ToList()
.ForEach(customerTelephone => client.SendMessage("12345678", customerTelephone, textMessage));
I would use Select instead of SelectMany:
SelectedStationCustomers
.Select(customer => String.Format("{0}", customer.CustomerTelephone))
.ToList()
.ForEach(customer => client.SendMessage("12345678", customer, textMessage));
customer will hold the CustomerTelephone value
Related
From this LINQ:
var inspectionItems = inspArchive.Select(x => x.InspectionItems);
I get this result in inspectionItems :
My question is how can I make using LINQ in elegant way to make from inspectionItems result above one array distincted by Id.
Like that:
You want the SelectMany() LINQ method:
var inspectionItems = inspArchive.SelectMany(x => x.InspectionItems);
If you only want distinct items by a particular property, then you can either implement an equality comparer to see if your inspection classes are equal, then call .Distinct(myInspectionEqualityComparer) at the end of your method chain, or you can do this:
var distinctInspectionItems = inspArchive.SelectMany(x => x.InspectionItems)
.GroupBy(i => i.Id)
.Select(group => group.First());
Without seeing more of your code, I think you want to use SelectMany and Distinct ?
var inspectionItems = inspArchive.SelectMany(x => x.InspectionItems).Distinct();
The question is unclear, but check if this works for you:
var inspectionItems = inspArchive.SelectMany(x => x.InspectionItems)
.GroupBy(x => x.ID)
.Select(g => g.First())
.ToArray();
The following works in VB..
Dim q = allValues.GroupBy(Function(u) u.R).Select(Function(grp) grp).OrderByDescending(Function(a) a.Count).ToList
But not in C#..
dynamic q = allValues.GroupBy(u => u.R).Select(grp => grp).OrderByDescending(a => a.Count).ToList;
"allValues" is a list of color of various pixels. I'm trying to group the color R value and sort by the count descending to find the most used color.
I'm a noob to C# and Lambda. This is also my first post to the site. Thanks for any insite!
a.Count is a method, it needs parentheses -
.OrderByDescending(a => a.Count())
So does ToList() for that matter.
And a couple of little side issue:
Select(grp => grp) is pointless, you can just remove that bit.
The result does not need to be dynamic - you can just use var.
The right syntax is
var q = allValues // probably you mean "var" instead of "dynamic"
.GroupBy(u => u.R)
.Select(grp => grp) // that's redundant, you can remove it
.OrderByDescending(a => a.Count())
.ToList();
note () after Count and after ToList. "()" means that you call the method.
I have a table and one of the properties of the table is TotalDue.I wish to first order it by TotalDue and then select the "top" record which in this case would be the record with the highest value.
homeVM.LastSaleAmount = (from i in salesService.GetSalesOrderHeaders()
.OrderByDescending(a => a.TotalDue).First();
This is what I've tried so far but I think .First() needs a parameter and I think I need a select as well but not really sure.
You can try with Take method, is like top, but in Linq world.
homeVM.LastSaleAmount = salesService.GetSalesOrderHeaders().OrderByDescending(a => a.TotalDue).Take(1);
https://msdn.microsoft.com/en-us/library/vstudio/bb503062%28v=vs.100%29.aspx
You're mixing method syntax and query syntax, and your use of query syntax isn't necessary and making this harder. Just remove it:
homeVM.LastSaleAmount = salesService.GetSalesOrderHeaders()
.OrderByDescending(a => a.TotalDue)
.Select(a => a.TotalDue)
.First();
You are trying to put an entire entity into LastSaleAmount. use .Select(a => a.TotalDue) like:
homeVM.LastSaleAmount = salesService.GetSalesOrderHeaders()
.OrderByDescending(a => a.TotalDue).Select(a => a.TotalDue).First();
I'm trying to do a GroupBy and then OrderBy to a list I have. Here is my code so far:
reportList.GroupBy(x => x.Type).ToDictionary(y=>y.Key, z=>z.OrderBy(a=>a.Lost));
With the help of the last question I asked on linq I think the ToDictionary is probably unneeded, but without it I don't know how to access the inner value.
To be clear, I need to GroupBy the Type property and want the inner groups I get to be OrderBy the Lost property (an integer). I want to know if there is a better, more efficient way or at the least better then what I've done.
An explanation and not just an answer would be very much appreciated.
Yes, there is better approach. Do not use random names (x,y,z,a) for variables:
reportList.GroupBy(r => r.Type)
.ToDictionary(g => g.Key, g => g.OrderBy(r => r.Lost));
You can even use long names to make code more descriptive (depends on context in which you are creating query)
reportList.GroupBy(report => report.Type)
.ToDictionary(group => group.Key,
group => group.OrderBy(report => report.Lost));
Your code does basically the following things:
Group elements by type
Convert the GroupBy result into a dictionary where the values of the dictionary are IEnumerables coming from a call to OrderBy
As far as the code correctness it is perfectly fine IMO, but maybe can be improved in term of efficiency (even if depends on your needs).
In fact, with your code, the values of your dictionary are lazily evaluated each time you enumerate them, resulting in a call to OrderBy method.
Probably you could perform it once and store the result in this way:
var dict = reportList
.GroupBy(x => x.Type)
.ToDictionary(y => y.Key, z => z.OrderBy(a => a.Lost).ToList());
// note the ToList call
or in this way:
var dict = reportList.OrderBy(a => a.Lost)
.GroupBy(x => x.Type)
.ToDictionary(y => y.Key, z => z);
// here we order then we group,
// since GroupBy guarantees to preserve the original order
Looks fine to me. If you use an anonymous type instead of a Dictionary, you could probably improve the readability of the code that uses the results of this query.
reportList.GroupBy(r => r.Type)
.Select(g => new { Type = g.Key, Reports = g.OrderBy(r => r.Lost) });
I am attempting to get distinct values from a datatable column. here is my code..
var sourceItems = ds.Tables[0].AsEnumerable()
.GroupBy(x => x.Field<string>("Source").ToString())
.Select(x => x.First());
ddlSource.DataSource = sourceItems;
ddlSource.DataBind();
This code is returning 4 rows of System.Data.DataRow as the values. I need the actual values stored in the rows not the row type. Also is this code a proper way to grab only distinct values in the column?
I would do something like this:
var sourceItems = ds.Tables[0].AsEnumerable()
.Select(x => x.Field<string>("Source"))
.Distinct()
.ToList();
Note that .ToList() can be skipped, but of course it depends on what the DataSource property of the ddlSource object is able to accept. If It's a winforms control I suspect ToList is necessary.
Your code does basically the same as this one, but you must change the last Select into Select(x => x.Key) to select the values that are used to group the rows, and not the first row.
Also your code has more overhead than using Distinct, since GroupBy creates subgroups of the original collection.
Why don't you use the .Distinct extension method?
ds.Tables[0].AsEnumerable()
.Select(x => x.Field<string>("Source").ToString())
.Distinct()
.FirstOrDefault();
How about
var sourceItems = ds.Tables[0].AsEnumerable()
.Select(x => x.Field<string>("Source"))
.Distinct()
.ToList();
ddlSource.DataSource = sourceItems;
ddlSource.DataBind();
(You don't need to call .ToString() on a string, so I've removed that.)