1

Sqlserver のビューを使用する代わりに、Linq クエリを使用します。メソッドに入れたいのですが、私の出力タイプは何ですか? 助けてくれてありがとう

public ???? InsuranceWorkerView()
{
    var InsuranceWorkers =(from ins_insuranceworkers in db.INS_InsuranceWorkers
         join dv in db.DivisionCountry on new { InsWorkerCityID = ins_insuranceworkers.InsWorkerCityID } equals new { InsWorkerCityID = dv.DivisionID } into divisioncountry_1_join
         from dv in divisioncountry_1_join.DefaultIfEmpty()
         join divisioncountry in db.DivisionCountry on new { InsWorkerStateID = ins_insuranceworkers.InsWorkerStateID } equals new { InsWorkerStateID = divisioncountry.DivisionID } into divisioncountry_join
         from divisioncountry in divisioncountry_join.DefaultIfEmpty()
         select new 
         {
            ins_insuranceworkers.InsWorkerID,
            ins_insuranceworkers.InsWorkerCreateUserID,
             ins_insuranceworkers.InsWorkerTypeID,
             ins_insuranceworkers.InsWorkerFirstName,
             ins_insuranceworkers.InsWorkerLastName,  
            ins_insuranceworkers.InsWorkerNationalCode,
             ins_insuranceworkers.InsWorkerMobileNum,
             ins_insuranceworkers.InsWorkerAddress,
             ins_insuranceworkers.InsWorkerEmail,
              City = dv.DivisionName,
              State = divisioncountry.DivisionName,
             InsWorkerStateID =(Int32?)ins_insuranceworkers.InsWorkerStateID,
             InsWorkerCityID = (Int32?)ins_insuranceworkers.InsWorkerCityID
         }).ToList();

    return InsuranceWorkers;
}
4

4 に答える 4

1

残念ながら、メソッドから匿名型を返すことはできません:

フィールド、プロパティ、イベント、またはメソッドの戻り値の型を匿名型として宣言することはできません。同様に、メソッド、プロパティ、コンストラクター、またはインデクサーの仮パラメーターを匿名型として宣言することはできません。匿名型、または匿名型を含むコレクションを引数としてメソッドに渡すには、パラメーターを型オブジェクトとして宣言します。

したがって、このデータの特定の型を作成して返します。

public List<Foo> InsuranceWorkerView()
{
    return (from i in db.INS_InsuranceWorkers
            select new Foo {
                InsWorkerID = i.InsWorkerID,
                InsWorkerCreateUserID = i.InsWorkerCreateUserID
                // etc
            }).ToList();
}

または戻りobjectます(これは少し役に立たないと思います):

public object InsuranceWorkerView()
{
    return (from i in db.INS_InsuranceWorkers
            select new {
                i.InsWorkerID,
                i.InsWorkerCreateUserID
                // etc
            }).ToList();
}
于 2013-10-26T12:46:06.557 に答える
0

新しいリストを IList として返すことができます。

public IList InsuranceWorkerView()
{
    var InsuranceWorkers =(from ins_insuranceworkers in db.INS_InsuranceWorkers
         join dv in db.DivisionCountry on new { InsWorkerCityID = ins_insuranceworkers.InsWorkerCityID } equals new { InsWorkerCityID = dv.DivisionID } into divisioncountry_1_join
         from dv in divisioncountry_1_join.DefaultIfEmpty()
         join divisioncountry in db.DivisionCountry on new { InsWorkerStateID = ins_insuranceworkers.InsWorkerStateID } equals new { InsWorkerStateID = divisioncountry.DivisionID } into divisioncountry_join
         from divisioncountry in divisioncountry_join.DefaultIfEmpty()
         select new 
         {
            ins_insuranceworkers.InsWorkerID,
            ins_insuranceworkers.InsWorkerCreateUserID,
             ins_insuranceworkers.InsWorkerTypeID,
             ins_insuranceworkers.InsWorkerFirstName,
             ins_insuranceworkers.InsWorkerLastName,  
            ins_insuranceworkers.InsWorkerNationalCode,
             ins_insuranceworkers.InsWorkerMobileNum,
             ins_insuranceworkers.InsWorkerAddress,
             ins_insuranceworkers.InsWorkerEmail,
              City = dv.DivisionName,
              State = divisioncountry.DivisionName,
             InsWorkerStateID =(Int32?)ins_insuranceworkers.InsWorkerStateID,
             InsWorkerCityID = (Int32?)ins_insuranceworkers.InsWorkerCityID
         }).ToList();

そして、アイテムにアクセスするには

   foreach(var item in InsuranceWorkers)
   {
      var a=   item.InsWorkerID; // to access the anonymous items
   }
}
于 2013-10-26T13:44:43.757 に答える
0

これは、動的が役立つ場合があります。

public IEnumerable<dynamic> InsuranceWorkerView()
{
    var InsuranceWorkers =(from ins_insuranceworkers in db.INS_InsuranceWorkers
         join dv in db.DivisionCountry on new { InsWorkerCityID = ins_insuranceworkers.InsWorkerCityID } equals new { InsWorkerCityID = dv.DivisionID } into divisioncountry_1_join
         from dv in divisioncountry_1_join.DefaultIfEmpty()
         join divisioncountry in db.DivisionCountry on new { InsWorkerStateID = ins_insuranceworkers.InsWorkerStateID } equals new { InsWorkerStateID = divisioncountry.DivisionID } into divisioncountry_join
         from divisioncountry in divisioncountry_join.DefaultIfEmpty()
         select new 
         {
            ins_insuranceworkers.InsWorkerID,
            ins_insuranceworkers.InsWorkerCreateUserID,
             ins_insuranceworkers.InsWorkerTypeID,
             ins_insuranceworkers.InsWorkerFirstName,
             ins_insuranceworkers.InsWorkerLastName,  
            ins_insuranceworkers.InsWorkerNationalCode,
             ins_insuranceworkers.InsWorkerMobileNum,
             ins_insuranceworkers.InsWorkerAddress,
             ins_insuranceworkers.InsWorkerEmail,
              City = dv.DivisionName,
              State = divisioncountry.DivisionName,
             InsWorkerStateID =(Int32?)ins_insuranceworkers.InsWorkerStateID,
             InsWorkerCityID = (Int32?)ins_insuranceworkers.InsWorkerCityID
         }).ToList();

    return InsuranceWorkers;
}

そして、次のようなものを使用できます。

foreach (var worker in InsuranceWorkerView()) {
   var id = (int)worker.InsWorkerID;
}
于 2013-10-26T13:36:05.080 に答える