I have the following code:
var request = new GeocodingRequest();
request.Address = postcode;
request.Sensor = "false";
var response = GeocodingService.GetResponse(request);
var result = response.Results. ...?
I'd very much like to get result as a list, but I can't seem to convert it. I know I can do something like response.Results.ToList<string>();, but have had no luck.
Can anyone help please :)
Well you can just use:
GeocodingResult[] results = response.Results;
or
List<GeocodingResult> results = response.Results.ToList();
If you want a list of strings, you'll need to decide how you want to convert each result into a string. For example, you might use:
List<string> results = response.Results
.Select(result => result.FormattedAddress)
.ToList();
It is defined as:
[JsonProperty("results")]
public GeocodingResult[] Results { get; set; }
if you want to make it list call: response.Results.ToList().
But why do you want to make it list? You can insert items into list, but I don't think you need it.
assuming response.Results is IEnumerable, just make sure System.Linq is available as a namespace and say response.Results.ToList()
Related
I need help, how do I get MAX datatable column value where value LIKE 'N01%'
Basically, if I convert this to SQL:
SELECT MAX(user) FROM tblUser WHERE user LIKE 'N01%'
Thank you.
You can simply do this:
string[] data = {"hello", "N01jaja", "N01ll"};
var userWithN1 = data.Where(we => we.StartsWith("N01")).Max();
StartsWith checks if the element starts with a certain string.
If there's a class then need to implement IComparable.
Sample code:
public class TestClass : IComparable<string>
{
public string Value { get; private set; }
public int CompareTo(string other) { return Value.CompareTo(other); }
}
var result = foo.tblUser.Where(u => u.user.StartsWith("N01")).Max(u => u.user));
Simply use a where statement to start your filter, use a StartsWith to emulate SQL's xx% pattern. Then use Max on a particular column. Though make sure User is something that will actually have a Max value.
In LINQ, I always find it helpful to break the problem down. Here in this case, you have a list of items, you want to narrow down that list with a WHERE clause and return the MAX of the remaining items.
Start
var myItems = db.GetMyList();
WHERE with LIKE
Assuming User is a string variable
myItems = myItems.Where(x=>x.User.StartsWith("N01"));
MAX
var maxItem = myItems.Max(x=>x.User);
All Together
var maxItem = db.GetMyList().Where(x=>x.User.StartsWith("N01")).Max(x=>x.User);
edit - Per comment below, since the search string was 'N01%', is should be starts with and not contains.
I have a foreach statement that outputs a list of customer id's to a log file:
foreach(var customer in _response.CustomersList)
{
log.WriteLine(customer.CustID);
}
The Id's output correctly but the problem I have is I am not able to put them into one variable use them.
For example I wanted to give this request: _request2.CustID = customer.CustID but this is not correct.
I need those ID's because I have a cancel customer request:
public void Cancel()
{
_request2 = new CancelCust();
_request2.CommandUser = _request.CommandUser;
_request2.CustID = "This is where I would put the variable that holds the customer ID's"
_request2.Company = _request.Company;
}
So, how do I assign those id's to a variable to be used in my request later?
I'm not totally clear on what you are trying to accomplish with the code above. BUT, you could use a bit of LINQ and get a list of the IDs, then pass around that list as you like:
var customerIDs = _response.CustomersList.Select(customer => customer.CustID);
You can get all the customer Ids by storing them as you go:
var customerIds = new List<int>();
foreach (var customer in _response.CustomersList)
{
customerIds.Add(customer.CustId);
log.WriteLine(customer.CustID);
}
or by using LINQ
var customerIds = _response.CustomersList.Select(c => c.CustId);
As I pointed out in my comment, if the property Request.CustId is not a collection this won't help you request all of them at once. Are you able to change the definition of Request?
It sounds like your variable _request2.CustID is the wrong type. What error message are you getting from the compiler? (Or, is it a run-time error that you get?)
Something like this?
public void Cancel()
{
foreach(var customer in _response.CustomersList)
{
_request2 = new CancelCust();
_request2.CommandUser = _request.CommandUser;
_request2.CustID = customer.CustID;
_request2.Company = _request.Company;
}
}
I have this code which will sometimes just have one string in it and sometimes have two. Basically when it has two strings I want to be able to select the second string as at the moment it is selecting the first string in the list.
Here is my code:
List<string> workGroupIdStringValues = new List<string>();
workGroupIdStringValues = (List<string>)session["WorkGroupIds"];
List<Guid> workGroupIds = workGroupIdStringValues.ConvertAll<Guid>(workGroupIdStringValue => new Guid(workGroupIdStringValue));
So "workGroupIdStringValues" will sometimes have a second string, how can I select the second and not the first when there is two strings. Is it possible, if so how?
Thanks
Use LINQ's workGroupIdStringValues.Last() to discard all strings but the last one; will work fine if there's just one string.
Update: And then of course you have to adapt the code somewhat:
var workGroupId = new Guid(((List<string>)session["WorkGroupIds"]).Last());
How about
string workGroupIdStringValue = ((List<string>)session["WorkGroupIds"]).Last();
Instead of
List<Guid> workGroupIds = workGroupIdStringValues.ConvertAll<Guid>(workGroupIdStringValue => new Guid(workGroupIdStringValue));
You can do following
Guid workGroupId = new Guid(workGroupIdStringValues[workGroupIdStringValues.Count-1]));
There are a few ways of doing this, I think the best is to do
workGroupIdStringValues.Count will give you the total amount of objects, and
workGroupIdStringValues[1] will return the second entry in your list.
So something like
if(workGroupIdStringValues.Count() > 1)
mystring = workGroupIdStringValues[1];
else
mystring = workGroupIdStringValues[0];
I am trying to read a file and process using LINQ.
I have a exclude list where if i encounter certain words in the file, i should omit that line
my code is
string sCodeFile = #"C:\temp\allcode.lst";
List<string> sIgnoreList = new List<string>() { "foo.c", "foo1.c" };
var wordsPerLine = from line in File.ReadAllLines(sCodeFile)
let items = line.Split('\n')
where !line.Contains(sIgnoreList.ToString())
select line;
foreach (var item in wordsPerLine)
{
console.WriteLine(item);
}
My LST file looks like below
\voodoo\foo.c
\voodoo\voodoo.h
\voodoo\std.c
\voodoo\foo1.h
in the end i want only
\voodoo\voodoo.h
\voodoo\std.c
How can i process the ignored list in contains? with my above code i dont get the desired output for sure
can any one help?
regards,
Karthik
Revised my answer. The bug is that you're doing a ToString on the ignore list, which certainly will not work. You must check each item in the list, which can be done using something like this:
where !sIgnoreList.Any(ignore => line.Contains(ignore))
A curiosity: since the above lambda is just passing a value into a method that only take the value as a parameter, you can write this even more compact as a method group like this:
where !sIgnoreList.Any(line.Contains)
Try this.
string sCodeFile = #"C:\temp\allcode.lst";
List<string> sIgnoreList = new List<string>() { "foo.c", "foo1.c" };
var wordsPerLine = File.ReadAllLines(sCodeFile).Where(n =>
{
foreach (var ign in sIgnoreList)
{
if (n.IndexOf(ign) != -1)
return false;
}
return true;
});
It passes the current element (n) to a lambda function, which checks it against every element of the sIgnoreList. Returning false means the element is ignored, true means it's returned.
Change it to:
where !sIgnoreList.Contains(line)
You need to compare each single line and check that it doesn't exist in the ignore list.
That's why the Vladislav's answer did not work.
Here's the working solution:
var result = from line in File.ReadAllLines(codeFile)
where !ignoreList.Any(line.Contains)
select line;
The problem was you didn't want to check for the whole path and messed up words/lines part a bit.
The following lines of code grabs a handful of rows from a database, and puts the value Table.Id into a list of integers. I imagine there is a way to condense this code into a single line, but I'm not sure how.
var result db.Table.Where(a=>a.Value>0).ToList();
List<int> ids = new List<int>();
foreach(var row in result){
ids.Add(row.Id);
}
Any help would be appreciated, thanks!
Edit: My title says array, my example has a list, either one is fine. Sorry if there was any confusion.
var ids = db.Table.Where(a => a.Value > 0).Select(row => row.Id).ToList();
Should do the trick unless my fu is off
Try this:
var result = db.Table.Where(a=>a.Value>0)
.Select(a=>a.Id)
.ToList();
You can also use the List.ForEach Method. So something like:
db.Table.Where(a=>a.Value>0).ToList().ForEach(delegate(Table row) { ids.Add(row.Id); });