2017-01-19 7 views
2

TableTypeで条件を適用したいと思います。Linqユースケース

If t.TableName='limit' then TabeType should be A_TypeTable 
If t.TableName='health' then TabeType should be B_TypePolicy 
If t.TableName='waiting' then TabeType should be C_TypeTable 

以下はクエリです。私は、ケースステートメントをどこに割り当てるかはわかりません。

var query = from a in tableA 
         join b in tableB on a.Hid equals b.Hid into ab 
         select new 
         { 

          TableType= ab.Select(t=>t.TableName).FirstOrDefault(), 
          UserName = a.UserName, 
          Description = a.Description, 
          ImportedDate = a.ImportedDate 

         }; 

いずれかの提案があります。これは、回避策かもしれないが、あなたは文字列としてテーブル名を受け入れて、文字列としてテーブル型を返すするメソッドを作成し、試して作ることができる

答えて

2

は、それがSQLの場合と条件を変換し、この使用して三項演算子のように試してみてください。

var query = from a in tableA 
           join b in tableB on a.Hid equals b.Hid into ab 
           let x= ab.Select(t => t.TableName).FirstOrDefault() 
           select new 
           { 

            TableType = x.equal("limit")? "A_TypeTable" : 
               x.equal("health") ? "B_TypeTable": 
               "C_TypeTable", 
            UserName = a.UserName, 
            Description = a.Description, 
            ImportedDate = a.ImportedDate 

           }; 
1

事前に おかげで、メソッドのシグネチャは次のようになります。

public string GetTableType(string tableName) 
{ 
    switch (tableName) 
    { 
     case "limit": 
      return "A_TypeTable"; 
     case "health": 
      return "B_TypePolicy";     
     default: 
      return "C_TypeTable"; 
    } 

} 

そして、あなたはこのようなあなたのクエリからメソッドを呼び出すことができます。

var query = from a in tableA 
        join b in tableB on a.Hid equals b.Hid into ab 
        select new 
        { 
         // Here is the call 
         TableType= ab.Select(t=>GetTableType(t.TableName)).FirstOrDefault(), 
         UserName = a.UserName, 
         Description = a.Description, 
         ImportedDate = a.ImportedDate 

        };