Edit: Removed anonymous type Edit: Added space in delimiter declaration to be a bit different :-) Hopefully I am not misunderstanding your question but you can add do it as follows by adding the delimiter in the return value of the query : public static List GetCitiesInCountryWithState(string isoalpha2) { const string delimiter = ", "; using (var ctx = new atomicEntities()) { var query = from c in ctx. Cities join ctry in ctx. Countries on c.
CountryId equals ctry. CountryId where ctry. IsoAlpha2 == isoalpha2 select c.
CityName + delimiter + c. State ; return query.ToList(); } } This will create a list of type string.
Edit: Removed anonymous type Edit: Added space in delimiter declaration to be a bit different :-) ", " Hopefully I am not misunderstanding your question but you can add do it as follows by adding the delimiter in the return value of the query : public static List GetCitiesInCountryWithState(string isoalpha2) { const string delimiter = ", "; using (var ctx = new atomicEntities()) { var query = from c in ctx. Cities join ctry in ctx. Countries on c.
CountryId equals ctry. CountryId where ctry. IsoAlpha2 == isoalpha2 select c.
CityName + delimiter + c. State ; return query.ToList(); } } This will create a list of type string.
1 Use String. Concat(string,string,string) – abatishchev Apr 10 at 11:29 1 He probably also wants a space between the comma after city and the state, Better to use string. Format("{0}, {1}",c.
CityName, c. State) – ryber Apr 10 at 11:33 1 @ryber Maybe, but he asked for "city and state, separated by a comma" and that's what I gave :-) – TBohnen. Jnr Apr 10 at 11:37 @ryber @TBohnen.
Jnr thanks both for your answers - I am indeed looking for a city and state separated by a comma AND a space. +1 to both for prompt comments and reading OP :) – dooburt Apr 10 at 11:44 @TBohnen. Jnr, either way, your answer works perfectly.
Thank you very much for speedy answer and detailed comments +1... – dooburt Apr 10 at 11:46.
Public static List GetCitiesInCountryWithState(string isoalpha2) { using (var context= new atomicEntities()) { return (from city in context. Cities join country in context. Countries on c.
CountryId equals country. CountryId where country. IsoAlpha2 == isoalpha2 select String.
Concat(city. CityName, ", ", city. State) // select String.
Format("{0}, {1}", city. CityName, city. State) ).ToList(); } } FYI You can also use String.
Join(string, IEnumerable) to get a result like this: IEnumerable GetCitiesInCountryWithState(string) IEnumerable r = GetCitiesInCountryWithState("100"); // { { "Miami, Florida" }, { "Key West, Florida" } } string s = String. Join("; ", r); // "Miami, Florida; Key West, Florida.
1 Would String. Concat result in a performance increase or why would you rather use that? – TBohnen.
Jnr Apr 10 at 11:34 This is very nice :) Shame I can't mark more than one answer correct. Thanks for your answer and comment however. +1 – dooburt Apr 10 at 11:52.
I use the Entity Framework as my DAL and in my BLL have lots of LINQ to format, move stuff, etc. I have on my UI a drop-down which currently accepts a ToList() from a method in my BLL and all works well, but I need to alter this method to return city and state, separated by a comma. I've tried numerous LINQ variations, but I'm obviously not getting this correct. Can anyone offer some help please?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.