简体   繁体   中英

Linq query to create list of string arrays error

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

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

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

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

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

在此输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM