Click here to Skip to main content
15,888,293 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to create a method that returns a query so I don't have to LINQ sort every time I need the results. LINQ code:
C#
public IEnumerable<String> queryReturn()
{
    string[] tr = staticGetTRdata();

    char[] splitChars = new char[] { ',' };

    var query = from data in tr
                let columns = data.Split(splitChars, StringSplitOptions.None)

                select new
                {
                    qNames = columns[0].Substring(0, columns[0].Length - 6),
                    totCallW8 = Int32.Parse(columns[3]),
                    longW8InCall = columns[5],
                    longW8CallBack = columns[6],
                    BGtrLWC = columns[9].Substring(8),
                    BGtrLCB = columns[10].Substring(8),
                }
                    into qSort
                    orderby qSort.qNames
                    select qSort;

    return query;
}


From what I found on the web so far my issue lies with the "select new" statement which creates the AnonymousType, but I can't seem to figure out how to go around that. What else should I be selecting?

Also I'm just assuming I'm returning IEnumerable<String> or should I return something else? I want to be able to get the query in another method and do the foreach loop to sort the results from there. How would I be calling it?

What I have tried:

Found a couple similar issues on StackExchange, but can't figure out how to implement them. Writing select data doesn't seem to get me anywhere. Just get error after error.
Posted

1 solution

Anonymous types can't really be used outside the function they are created in. Create a concrete class with the properties you need and return an IEnumerable<T> of that class instead.
 
Share this answer
 
Comments
[no name] 10-Feb-16 6:15am    
Ok I think I got it! I created the class qData which has the vars with the {get;set;} and then call it by IEnumerable<qdata> querey = getQdata(); Now in my foreach loop is there a way to say something like using result so that from there instead of typing result.qName I can just type qName.....?
Sascha Lefèvre 10-Feb-16 6:44am    
No, there's no "with"-keyword in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900