2

私は一日中検索していて、これに対する解決策を見つけることができません...

EntityCollectionそれぞれがCommunicationオブジェクトのインスタンスIntention(1対1)を持つオブジェクトがあります。

(1対多)のUserインスタンスが多数あるオブジェクトもありますUserLocation EntityObjects

  • IntentionオブジェクトにはプロパティがありますUID
  • UserLocationオブジェクトにはプロパティがありますLID

  • オブジェクトに関連付けられたインスタンスのプロパティが、オブジェクトのインスタンスの任意のインスタンスの任意のプロパティと等しいすべてのCommunicationオブジェクトを返すLINQ式を記述したいと思います。UIDIntentionCommunicationLIDUserLocationUser

私はこれを試しました

return _context.Communications.Where
(u => u.Intention.UID.Equals
(user.UserLocations.Select
(p => p.LID)));

この

return _context.Communications.Where
(u => user.UserLocations.Any
(x => x.LID.Equals
(u.Intention.UID)));

この

var thislist = from Intentions in _context.Intentions
                           join UserLocations in user.UserLocations
                           on Intentions.UID equals UserLocations.LID
                           select Intentions.UID;
            return _context.Communications.Where(u => u.Intention.Equals(thislist.Any()));

この

var lidlist = user.UserLocations.Select(x => x.LID);
return _context.Communications.Where(x=> lidlist.Contains(x.Intention.UID)).ToList();

(これにより、Containsステートメントで「DelegateSystem.Func<Communication,int,bool>は1つの引数を取りません」というエラーが発生します。修正方法がわかりません)

これらすべてのバリエーションに加えて、私も持っています:

  • メソッドを変更して戻り、クエリに追加IQueryable<Communication>しようとしました。List<Communication>ToList()

何も機能しません。私が何をしようとしても、私はいつもこの例外で終わります

NotSupportedExceptionはユーザーコードによって処理されませんでした

タイプ'PreparisCore.BusinessEntities.UserLocation'の定数値を作成できません。このコンテキストでは、プリミティブ型('Int32、String、Guid'など)のみがサポートされます。

私は何が間違っているのですか?

4

2 に答える 2

1

このコードを考えると:

namespace CollectionsWithIntentions
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;

    internal class Program
    {
        #region Methods

        private static void Main(string[] args)
        {
            var communications = new[]
                {
                    new Communication { Intention = new Intention { UID = 1 } },
                    new Communication { Intention = new Intention { UID = 2 } },
                    new Communication { Intention = new Intention { UID = 3 } },
                    new Communication { Intention = new Intention { UID = 4 } },
                };
            var users = new[]
                {
                    new User { UserLocations = new List<UserLocation>(new[] { new UserLocation { LID = 2 },new UserLocation{LID=5}  }) },
                    new User { UserLocations = new List<UserLocation>(new[] { new UserLocation { LID = 3 } }) }
                };

            IEnumerable<Communication> res =
                communications.Where(w => users.Any(a => a.UserLocations.Any(b=>b.LID == w.Intention.UID)));
            foreach (Communication communication in res)
            {
                Trace.WriteLine(communication);
            }
        }

        #endregion
    }

    internal class Communication
    {
        #region Public Properties

        public Intention Intention { get; set; }

        #endregion

        #region Public Methods and Operators

        public override string ToString()
        {
            return string.Concat("Communication-> Intention:", this.Intention.UID);
        }

        #endregion
    }

    internal class Intention
    {
        #region Public Properties

        public int UID { get; set; }

        #endregion
    }

    internal class User
    {
        #region Public Properties

        public List<UserLocation> UserLocations { get; set; }

        #endregion
    }

    internal class UserLocation
    {
        #region Public Properties

        public int LID { get; set; }

        #endregion
    }
}

私はこの結果を得る:

Communication-> Intention:2
Communication-> Intention:3

私は何かが足りないのですか?

于 2012-07-30T21:20:06.873 に答える
0

コメントの1つにリンクした最後の2つのコンパイラエラーから...

ここに画像の説明を入力してください

...コメントで述べたように、これはnull許容型でありIntention.UIDnull許容int?ではないと結論付けます。intこれは確かにコンパイルされません。最後のクエリを次のように変更してみてください。

var lidlist = user.UserLocations.Select(x => x.LID);
return _context.Communications
    .Where(x => x.Intention.UID.HasValue
             && lidlist.Contains(x.Intention.UID.Value))
    .ToList();

他の3つのクエリはuser.UserLocations、メモリ内の非プリミティブカスタムタイプのコレクションであり(生成されるSQLクエリの場合は「定数」値です)、EFはそのようなSQLクエリの作成をサポートしていないため機能しません。定数カスタムタイプ。

于 2012-07-31T13:32:24.920 に答える