开发者

send Iqueryable as parameter To Function

I Send Iqueryable to a function that is below

void Method<T>(ref IQueryable<T> qu)
{
    foreach (T item in qu)
    {

    }
}

because i need use from tis fu开发者_如何学编程nction for some diffrent Iqueryable and call this method with

Patient_BusinessEntity Be = new Patient_BusinessEntity();
IQueryable<PatientDataAccess> Query = Be.GetAllRow();
Method<PatientDataAccess> (ref Query);

his work Successfully

now i want use from property of item(variable of loop) in loop

how to can i do this ?

Thanks IFA


First, is there any reason to make it a ref parameter? If you want to return a new value, using the return value of the method is almost always better than using a ref parameter.

Now, it's a generic method - how do you know which property you want to access in the loop? If it's fixed and you know that T will always be some subtype of something which declares the property, you can just add a constraint:

void Method<T>(IQueryable<T> query) where T : SomeType
{
    foreach (T item in query)
    {
        int size = item.Size; // Size declared in SomeType
        // ...
    }
}

If you only know it at execution time - or if it's not declared by any common base type - you'll have to use reflection to get at it... or you could possibly pass it into the method:

voidMethod<T>(IQueryable<T> query, Func<T, int> sizeSelector)
{
    foreach (T item in query)
    {
        int size = sizeSelector(item);
        // ...
    }      
}

If you could give more information about what you're trying to do, we may be able to help you more.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜