ErocM ErocM - 1 month ago 5x
C# Question

Convert linq query to Generic string array

I know that I can cast my linq query to an array or list but this doesn't seem to help.

Here is my query:

var bracct = from DataRow x in _checkMasterFileNew.Rows
select new {BranchAccount = string.Format("{0}{1}", x["Branch"], x["AccountNumber"])};

When I attempt to convert it to a list or array:

List<string> tstx = bracct.ToList();

or this:

string[] stx = bracct.ToArray();

If give me this:

enter image description here

I am assuming I need to change my query but I'm not sure the best way to hanlde it. How do I get it to a generic collection of strings?


Your query is creating an anonymous type with a single member BranchAccount. If you actually just want a string, then just select that instead:

var bracct = 
    from DataRow x in _checkMasterFileNew.Rows
    select string.Format("{0}{1}", x["Branch"], x["AccountNumber"]);

And now your ToList() call will return List<string>:

List<string> tstx = bracct.ToList();