demo_user
demo_user

Reputation: 461

How to insert the LINQ result to a string[] array

I have a one dimensional string[] array as follows

string[] header = new string[30];

    public class GetList
    {
        public string ServerID;
        public string Name;

        public GetList(string sName, string cName)
        {
            this.ServerID = sName;
            this.Name = cName;
        }
    }        

and I have a query which will return the list of server names and component names as follows

   var query = (from a in this.hmdb.Servers
                join b in this.hmdb.Components
                on a.ServerID equals b.ServerID
                select new { a.ServerID, b.Name});

how to insert the query result into the string[] header ?

EDIT

when i tried something like this

  var query = (from a in this.hmdb.Servers
                     where a.ServerID.Contains(match)
                     join b in this.hmdb.Components
                     on a.ServerID equals b.ServerID
                     select new
                         {
                             ID = a.ServerID,
                             Name = b.Name
                         }).ToArray();

I get the result as list of ServerID and Names

UPDATE

here is the explanation for the marked answer..

what's happening is that it will create a queryable/list of ServerID and Name then convert it to Enumerable and create a List of string using the .Select() extension and convert it to array of string select new { a.ServerID, b.Name } --> creates a list/iqueryable of anonymous type that has ServerID and Name properties..

AsEnumerable() --> convert it to Enumerable so we can use string.Format because SQL to LINQ doesn't support string.Format

Select(x => string.Format("{0} - {1}", x.ServerID, x.Name)) --> do a Select in order to create a list of String using the ServerID and Name

ToArray() --> simply convert it to a String[] there.

Upvotes: 2

Views: 10172

Answers (3)

aiapatag
aiapatag

Reputation: 3430

Try creating a new string and converting it to an array of string in your select.

   var query = (from a in this.hmdb.Servers
                join b in this.hmdb.Components
                on a.ServerID equals b.ServerID
                select new {a.ServerID, b.Name}).AsEnumerable().Select(x=> string.Format("{0} - {1}",x.ServerID, x.Name)).ToArray();

   string[] header = query;

Upvotes: 5

ken2k
ken2k

Reputation: 48975

Only parameterless constructors and initializers are supported in LINQ to Entities.

Use:

....
on a.ServerID equals b.ServerID
select new GetList { ServerID = a.ServerID, Name = b.Name});

Upvotes: 2

Sachin
Sachin

Reputation: 40970

This will return an array of object GetList

 select new GetList(a.ServerID, b.Name)).ToArray();

if you want to return string array then choose the field which you want to return as array of string

 select new GetList(b.Name)).ToArray();

or

select new GetList(a.Server + b.Name)).ToArray();

Upvotes: 1

Related Questions