2

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.

12
  • You have an array of string, but you deal with a GetList class. Doesn't match. Provide a way to convert a GetList object to string, or use an array of GetList objects. Commented Jun 14, 2013 at 10:24
  • I have removed the class and edited using only select new {} . kindly check .. Commented Jun 14, 2013 at 10:26
  • header = query.toArray(); Commented Jun 14, 2013 at 10:26
  • @JibranKhan I tried it already .it didnt work for me Commented Jun 14, 2013 at 10:27
  • 1
    Then check out this lin telerik.com/help/openaccess-orm/… Commented Jun 14, 2013 at 10:29

3 Answers 3

5

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;
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the update, but i get the following exception as "LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression" - NotSupportedException
do I have to map the query to the header[] string ?
@demo_user yes, but im afraid that it could overflow since your array header has a limit of 30, why not just make it string[] header; and then header = query?
yes, Ihave already made it like that.. but still i have the same error :( - Only parameterless constructors and initializers are supported in LINQ to Entities.
weird. it should work man. how about using anonymous type. look at my edit again @demo_user
|
2

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});

2 Comments

thanks @ken2k.. but should it be like this select new GetList (ServerID = a.ServerID, Name = b.Name)); ? and I still have an error on ServerID saying the Name ServerID does not exist in the current context, through I have defined in my class
pls see my edit in the question. I get the list of IDs and Names. my concern is how to save this result in a string[] array? do i have to use foreach ?
1

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();

1 Comment

he wants to have a fixed size array with 30 items

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.