4

2 つのエンティティの拡張メソッドを作成しようとしています。

最初にオブジェクトのタイプを見つけてから、inner join別のテーブルで実行します。

A型JoinならBと一緒、B型ならAと合流Join

public static C GetAllInfo<T>(this IQueryable<T> objCust)
{
    if (typeof(T) == typeof(B))
    {
        //prepare the Object based on the Type 
        var objCastReg = objCust as IQueryable<B>;
        //how to write join here   ?????
        var  objUsermaster=objCastReg.GroupJoin(A,um=>um.UserId,r=>r.)

       //Build the Class object  from two retrieved objects.
    }
    if (typeof(T) == typeof(A))
    {
        var objCast = objCust as IQueryable<A>;
    }
    return null;
}

public class C
{
    public A A{ get; set; }

    public B B{ get; set; }
}
4

1 に答える 1

6

ジェネリックをまったく使用しないように思えます。ジェネリックは、ジェネリック メソッドが型を知る必要がない場合に使用します。ジェネリック型パラメーターは、このメソッドがどのような具体的な型でも機能することを示します。

ここでは、両方のケースに対して2つの特別なケースのメソッドが必要な場合があります。これにより、すべてのキャストと複雑さが解消されます。

ただし、一般的な方法を主張する場合は、次のようにします。最初に、私が話していた特別なケースのメソッドを作成します。

public static C GetAllInfo(this IQueryable<A> objCust); //implement this
public static C GetAllInfo(this IQueryable<B> objCust); //implement this

次に、それらに委任します。

public static C GetAllInfo<T>(this IQueryable<T> objCust)
{
    if (typeof(T) == typeof(B))
    {
        return GetAllInfo((IQueryable<B>)objCust); //call to specialized impl.
    }
    if (typeof(T) == typeof(A))
    {
        return GetAllInfo((IQueryable<A>)objCust); //call to specialized impl.
    }
    //fail
}
于 2013-08-07T12:23:24.083 に答える