How can I return IQueryable to C # 3.0 without knowing its type?

I have a method in which I execute an SQL query (I use Linq for SQL, but I need to execute a classic SQL query), but I do not know from which table / Entity this query will be created.

So, I thought that since I don’t know which table the query will be generated from, I don’t know the type of my IQueryable, am I correct?

But I do not know how to return this IQueryable? I tried to return <T>, but it does not work.

Thanks!

+3
source share
2 answers

IQueryable<T> , . , IQueryable<T>, # Type Inference , T . , Linq , , .

, , , :

public static IQueryable<T> FilterDefaults<T>(IQueryable<T> theSet)
{
    IQueryable<T> query =
        from t in theSet
        where t != default(T)
        select t;

    return query;
}

, #, <T>, T , :

string[] myStrs = { "ABC", null, "", "123", null, "XYZ" };
var theFilteredStrs = FilterDefaults(myStrs);
// should represent the sequence: "ABC", "", "123", "XYZ"
// note null is missing because default(string) is null

int[] myNums = { -1, 0, 3, 0, 42 };
var theFilteredNums = FilterDefaults(myNums);
// should represent the sequence: -1, 3, 42
// note 0 is missing because default(int) is 0

, , LINQ ( LINQ-to-SQL), LINQ. , LINQ-to-SQL LINQ-to-Objects.

+1

, . , (.. ) , ( , Linq to Sql) (, Class Struct) ... , , , .

,

0

Source: https://habr.com/ru/post/1708103/


All Articles