0

I want the output as the below string format using LINQ.

["providerZip","providerType"]

Can anyone please tell me, how to concatenate the two strings in an array using LINQ ?

3
  • 2
    Any code snippet? What have you tried so far? Commented Oct 13, 2015 at 5:30
  • 2
    string.Join is usually the best way to join an IEnumerable into a comma separated list. You don't need LINQ for that. If this is even your question. Commented Oct 13, 2015 at 6:16
  • list[0] + list[1]. I KNOW it's not LINQ, but I just couldn't resist Commented Oct 13, 2015 at 6:34

4 Answers 4

1

try this way

Contex.Tableuser.select{x=>new{Provider=x.ProviderZip+" "+x.ProviderType }}.ToList()
Sign up to request clarification or add additional context in comments.

Comments

1
using System.Linq;
using System;

public class Program
{
    public static void Main()
    {
       string[] words = { "providerZip", "providerType" };
        var res = words.Aggregate((current, next) => current + ", " + next);
        Console.WriteLine(res);

    }
}

1 Comment

I am getting an error message as "cann't convert string to char
0

You can try like this:

string[] str = { "providerZip", "providerType" };
var res = str.Aggregate((cur, nxt) => cur + ", " + nxt);

2 Comments

I am getting an error message as "cann't convert string to char"
@AnandaPrasadJaisy:- Can you share your code? Without that it is hard to tell what you are doing?\
0

What you actually need to Json Serialization of a list of strings. That'd be the right way to go. For Json I've used Json.NET in my example below, you can use the .NET json serializer as well.

But, if really don't want that, you can still manually create that string using string.Join and a little Linq.

//input strings
var str = new List<string>{ "providerZip", "providerType" };

//with json serialization with Json.NET 
//using Newtonsoft.Json; <= install Json.Net NuGet package
var jsonString = JsonConvert.SerializeObject(str); //=> ["providerZip","providerType"]

//with plain string manipulation + Linq
var outputString = "[" + string.Join(",", str.Select(s => "\"" + s + "\"")) + "]"; //=> ["providerZip","providerType"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.