返回一个对象集合,其中objects属性与使用LINQ-to-Entities的另一个对象集合中的任何属性匹配

时间:2012-07-30 20:53:04

标签: c# linq entity-framework linq-to-entities entitycollection

我一整天都在寻找,但找不到解决方案...

我有EntityCollectionCommunication个对象,每个对象都有一个Intention对象的实例(一对一)。

我还有一个User对象,其中包含UserLocation EntityObjects(一对多)的多个实例

  • Intention个对象具有属性UID
  • UserLocation个对象具有属性LID

  • 我想编写一个LINQ表达式,它返回所有Communication个对象,其中与UID对象关联的Intention实例的Communication属性等于ANY { {1}} LID对象的UserLocation实例的任何实例的{1}}属性。

我试过这个

User

和这个

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()));

(这在Contains语句中给出了一个错误,说“委托var lidlist = user.UserLocations.Select(x => x.LID); return _context.Communications.Where(x=> lidlist.Contains(x.Intention.UID)).ToList(); 不接受1个参数”,不知道如何解决)

除了所有这些变化,我还有:

  • 修改了我的方法以返回System.Func<Communication,int,bool>,并在向我的查询追加IQueryable<Communication>时尝试List<Communication>

没有任何作用。无论我尝试什么,我总是最终得到这个例外

NotSupportedException未由用户代码

处理

无法创建“PreparisCore.BusinessEntities.UserLocation”类型的常量值。在此上下文中仅支持原始类型(例如Int32,String和Guid')。

我做错了什么?

2 个答案:

答案 0 :(得分: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

我错过了什么吗?

答案 1 :(得分:0)

从您在其中一条评论中链接的最后两个编译器错误...

enter image description here

...我会得出结论,Intention.UID可以为空的类型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();

其他三个查询不起作用,因为user.UserLocations是内存中非原始自定义类型的集合(对于要生成的SQL查询,它是“常量”值)并且EF不支持使用这种常量自定义类型构建SQL查询。