1

I am trying to create a list of string arrays. Below is the code.

public List<string[]> GetRec(int hid)
        {
            var list =
            (from x in table1
                join y in table2 on x.Hid equals y.Hid       
                where x.Hid == hid
                select new[] {x.name, x.Description, x.user}).ToList();
            return list;
        }

But i am getting the following error

"The array type 'System.String[]' cannot be initialized in a query result.
 Consider using 'System.Collections.Generic.List`1[System.String]' instead."

Can anyone suggest me what is wrong here. Any help would be appreciated .

Thanks in advance

5
  • 1
    Simple, don't return an array when a record will do. Create a class to hold the values or use a tuple. Commented Jan 19, 2017 at 1:08
  • Why do you think you need to use an array? Commented Jan 19, 2017 at 1:18
  • I want to add multiple strings Commented Jan 19, 2017 at 1:20
  • Are the properties/fields of x declared as strings? Commented Jan 19, 2017 at 1:25
  • yes everything is string Commented Jan 19, 2017 at 1:26

4 Answers 4

3

Can anyone suggest me what is wrong here

The answer is inside the first part of the error message:

The array type 'System.String[]' cannot be initialized in a query result.

It's simply telling you that you cannot use array inside query select clause (if you ask why, I don't know, and it really doesn't matter - the only important part is that it is a requirement of the library you are using).

So, the wrong part is

select new[] { x.name, x.Description, x.user }

and the solution is inside the second part of the error message:

Consider using 'System.Collections.Generic.List'1[System.String]` instead."

Effectively it's telling you to use List instead of array. Thus, either

select new[] { x.name, x.Description, x.user }.ToList()

or

select new List<string> { x.name, x.Description, x.user }

will solve the SQL query translation part (eliminate the exception).

To get the desired final result, you should switch to LINQ to Objects context by using AsEnumerable() and convert the result lists to arrays:

var list =
    (from x in table1
     join y in table2 on x.Hid equals y.Hid       
     where x.Hid == hid
     select new [] { x.name, x.Description, x.user }.ToList())
    .AsEnumerable()
    .Select(x => x.ToArray())
    .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

sir, can you see my answer, As I have do same as asked in question and I don't have any exceptions !
@AmitMaheshwari It really depends of what the table1 and table2 variables are. You are creating List and of course there is no exception. However if you use database table and Entity Framework, you'll get the exact same exception. In other words, the exception is specific for one of the query providers - you don't believe that if something works in LINQ to Objects, it will work with all query providers, do you :)
Worked great, just ensure all the values you select are strings or converted to strings.
0

I think you are opting a failure approach here to solve this, you can try something like the following by creating a Class for the new items.

New class definition:

public class LinqResult
 {
     public string Name { get; set; }
     public string Description { get; set; }
     public string User { get; set; }
 }

Changes in the method signature:

public List<LinqResult> GetRec(int hid)
 {
     List<int> intList = new List<int>() { 1, 2, 5, 8, 9, 6, 3, 2, 4, 5, 6, 3, 2, 4, 855, 6, 2, 4, 6, 1, 56, 3 };
     var list = (from x in table1
                 join y in table2 on x.Hid equals y.Hid
                 where x.Hid == hid
                 select new LinqResult
                 {
                     Name = x.name,
                     Description = x.Description,
                     User = x.user
                 }).ToList();
     return list;
 }

Example usage:

foreach (LinqResult item in GetRec(10))
{
    Console.WriteLine("Name : {0} \n Description : {1} \n User : {2}", item.Name, item.Description, item.User);
}

1 Comment

Again I'll ask why you feel you have to use an array. And your answer "because I want to add multiple strings" doesn't have any incidence on your decision to use an array. Many other structures could be used: a class, a List of List and so on. So again, why do you think it must be an array ?
0

Hmmm

This work maybe?

public List<string[]> GetRec(int hid)
{
    var list =
        (from x in table1
            join y in table2 on x.Hid equals y.Hid       
            where x.Hid == hid
            select new {x.Name, x.Description, x.User})
        .Select(a => { return new string[] { a.Name, a.Description, a.User}; }).ToList();
    return list;
}

Comments

0

Similar kind of program I've made. So, I don't think you need to make any wrapper class. Even you don't have to use a list instead of an Array.

public class Records
    {
        public string Name;
        public int Number;
        public Records(string ticker, int number)
        {
            this.Name = ticker;
            this.Number = number;
        }
    }


static void Main(string[] args)
{
            List<Records> list = new List<Records>();
            list.Add(new Records("amit", 1));
            list.Add(new Records("bharat", 1));
            list.Add(new Records("amit", 2));
            list.Add(new Records("bhart", 2));


            List<string[]> listStr = (from ele in list
                                      where ele.Name == "amit"
                                      select new[] { ele.Name, ele.Number.ToString() }).ToList();
}

This code gives me exact result what I want

enter image description here

So I think you need to see more in your code, there must be something wrong. one thing you can do is, try this code in your environment to get confirmed that it is not because of your target framework or linq's dll (however it doesn't seem so, as I have tried this in both 3.5 and 4.0)

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.