问题描述:

I have a LINQ query whom i want to return its rows.

the perferred output would be a list of the rows, or an array.

var Cases = from q in db.Cases

where q.Company == Company

select q;

This will get the cases i want, but i want my method to return either an array or a list of the rows.

Many google hits suggest using SelectMany(); but i keep getting errors with it.

eg :

public static List<string> ReceiveDBlist(string Company)

{

var Cases = from q in db.Cases

where q.Company == Company

select q;

List<string> lst = Cases.SelectMany(x => x).ToList();

return lst;

}

Gives this error :

The type arguments for method

System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable,

System.Func>)

cannot be inferred from the usage. Try specifying the type arguments

explicitly.

Sort of like this

0 : column1;column2;column3

1 : column1;column2;column3

2 : column1;column2;column3

so when i receive my list, i can disect each row, and then seperate the data.

网友答案:

You can try another approach:

public static List<string> ReceiveDBlist(string Company)
{
    List<string> result = new List<string>();

    var Cases = from q in db.Cases
                where q.Company == Company
                select q;

    foreach(var c in Cases)
    {
        string row = c.Column1 + ";" + c.Column2 + ";" + c.Column3;
        result.Add(row);
    }

    return result;
}
网友答案:

That is because Cases is not a string, so you need to select the exact property of Cases, e.g. If you had a property CaseTitle you could then just use .ToList() like this:

public static List<string> ReceiveDBlist(string Company)
{
    var Cases = from q in db.Cases
                where q.Company == Company
                select q.CaseTitle;

    return Cases.ToList();
}
网友答案:

Select the data that you want in the query, and just use the ToList method on the query:

var Cases = from q in db.Cases
    where q.Company == Company
    select q.Name;

List<string> lst = Cases.ToList();
相关阅读:
Top