Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to pass the result of Linq as a Parameter
Message
Information générale
Forum:
ASP.NET
Catégorie:
LINQ
Divers
Thread ID:
01520475
Message ID:
01520478
Vues:
51
>I have a piece of Linq Code as:
>
>var queryRep =
> (
> from rows in dt.AsEnumerable()
> where rows.RowState != DataRowState.Deleted
> group rows by new { Rep = rows.Field string ("Rep") }
> into myGroup
> select new { Rep = myGroup.Key.Rep }
> );
>
>This 'Creates' a queryRep which I now want to pass into another method, and use it. What is the syntax for doing this
>e.g.
>
>MyMethod(queryRep)
>
>public void MyMethod(xxx queryRep)
>{
> // do stuff with the output from the linq statement
>}

I don't think you can do this (at least not simply) if the query uses an anonymous type. You could use a concrete type instead. Something like:
public class result
    {
        public result(string s)
        {
            Rep = s;
        }
        public string Rep { get; set; }
    }
then (simpler example):
var queryRep = from rows in dt.AsEnumerable() select new result(rows.Field<string>("Name"))
;
MyMethod(queryRep);
This should enable you for example to call:
void MyMethod(System.Data.EnumerableRowCollection result query)
        {
           foreach (result r in query)
           {
               Console.WriteLine(r.Rep);
           }
        }
Note: the angle brackets around the 'result' in the MyMethod parameters don't show up (as is the case in your Rows.Field code) - I think this may be what you are asking about in the UT thread.

Also bear in mind that when the query actually executes that the DataTable will need to be in scope. TBH, I have a gut feeling this is not a good approach. If you explain why you want this to actually execute in a different method maybe we can find an alternative...
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform