0

linq を使用して、別のオブジェクト リストをデータメンバーとして含むオブジェクトのリストを返そうとしています。示されている例を試しましたが、試行ごとに異なるエラーが発生し続けます。その 1 つは次のとおりです。LINQ to Entities はメソッド 'System.Collections.Generic.List 1[SunGard.Tools.Notifications.LinkVariable] ToList[LinkVariable](System.Collections.Generic.IEnumerable1[SunGard.Tools.Notifications.LinkVariable])' メソッドを認識せず、このメソッドはストア式に変換できません。

いくつかの文字列データメンバーと別のオブジェクトのリスト (List) を含むオブジェクト (AlertMessageReturn) があります。LinkVarible を定義するクラスと、値を含むテーブルがあります。私のクエリは次のようになります。

AlertMessagesQuery = from alertMessage in this.context.AlertMessages
where alertMessage.UserId=UserId                                              
select new AlertMessageReturn()
{ PAM_ShortMessage = alertMessage.PAM_ShortMessage,
PAM_LongMessage = alertMessage.PAM_LongMessage,
PAM_LongMessageRemote = alertMessage.PAM_LongMessageRemote,
LinkVariables = (from linkVariable in this.context.AlertMessageLinks
from user in this.context.AlertMessageUsers
 where user.PAMU_PAM_ID == linkVariable.PAML_PAM_ID && user.PAMU_UserId == UserId
 select new LinkVariable()
  {
  Name = linkVariable.PAML_SessionVariableName,
  Value = linkVariable.PAML_SessionVariableValue
   })
};

このエラーは、リンク変数に対して返される型に関連しています。助けてください。

次のようにコードを変更しました。

LinkDataQuery = from linkData in this.context.AlertMessageLinks
   from user1 in this.context.AlertMessageUsers
    where user1.PAMU_PAM_ID == linkData.PAML_PAM_ID && user1.PAMU_UserId == UserId
     select new LinkData
       {
        Name = linkData.PAML_SessionVariableName,
          Value = linkData.PAML_SessionVariableValue
         };
var links = LinkDataQuery.ToList();

    AlertMessagesQuery = from alertMessage in this.context.AlertMessages
where alertMessage.UserId=UserId                                              
select new AlertMessageReturn()
 { PAM_ShortMessage = alertMessage.PAM_ShortMessage,
   PAM_LongMessage = alertMessage.PAM_LongMessage,
   PAM_LongMessageRemote = alertMessage.PAM_LongMessageRemote,
    LinkVariables = links
 };


var AlertMessages = AlertMessagesQuery.ToList();  // this is where the error point to
                        if (AlertMessages.Any())
                        {
                            return AlertMessages;
                        }

System.NotSupportedException: Unable to create a constant value of type 'SunGard.Tools.Notifications.LinkData' というエラーが表示されます。このコンテキストでは、プリミティブ型 (Int32、String、および Guid など) のみがサポートされます。

4

3 に答える 3

0

「ストア式に変換できない」タイプのメッセージを受け取った場合は常に、他のステートメント(通常はSQL)に変換しようとしているlinqで何かを実行していることを示します。たとえば、あなたが言うなら

....select new MyObject
    {
         Id = Guid.Parse( passedIdentity ),
         ....
    }

これは完全に有効なC#ステートメントですが、Guid.Parseをlinqで処理できないというエラーが発生します。クエリ内で使用される外部変数に変数を移動できる場合は、機能します。だからあなたは...

string name = linkVariable.PAML_SessionVariableName;
string nValue = ....
....
select New LinkVariable
{
    Name=name,
    Value=nValue
};

また...SelectNewステートメントのクロージングパレンは必要ありません。

于 2012-06-13T15:38:55.563 に答える
0

LINQ to SQL エンジンは、サブクエリを生成しLinkVariablesて SQL に変換できません。さらに重要なことに、SQL はそのようなネストされたデータ セットを返すことができません。

于 2012-06-13T15:30:40.203 に答える
0

LINQ to SQL はオブジェクト階層を戻すことができますが、モデルの一部ではない型に射影することはできません。AlertMessageReturn 型に射影する代わりに、コードの IQueryable 部分で匿名型に射影してみてください。データベース クエリの構造化が完了したら、(AsEnumerable を使用して) 結果を強制的に返してから、それを AlertMessageReturn 型に射影します。オーバーヘッドは増えますが、機能します。または、AutoMapper などを使用して、匿名型を結果型に変換することもできます。

    AlertMessagesQuery = 
      from alertMessage in this.context.AlertMessages 
      where alertMessage.UserId=UserId                                               
      select new 
      {
         alertMessage.PAM_ShortMessage, 
         alertMessage.PAM_LongMessage, 
         alertMessage.PAM_LongMessageRemote, 
         LinkVariables = from linkVariable in this.context.AlertMessageLinks 
                         from user in this.context.AlertMessageUsers 
                         where user.PAMU_PAM_ID == linkVariable.PAML_PAM_ID && user.PAMU_UserId == UserId 
                         select new  
                         { 
                             Name = linkVariable.PAML_SessionVariableName, 
                             Value = linkVariable.PAML_SessionVariableValue 
                         }) 
      }; 
    var alertMessageResults = 
      from message in AlertMessagesQuery.AsEnumerable()
      select new AlertMessageResult 
      {
        PAM_ShortMessage = mesage.PAM_ShortMessage,
        PAM_LongMessage = message.PAM_LongMessage,
        PAM_LongMessageRemote = message.PAM_LongMessageRemote,
        LinkVariables = (from variable in message.LinkVariables
                        select new LinkVariable { Name=variable.Name, Value = variable.Value})
                        .ToList()
      };
   return alertMessageResults.ToList();
于 2012-06-13T18:59:01.047 に答える