使用LINQ-to-Entities返回对象集合,其中对象属性与另一个对象集合中的任何属性匹配 [英] Returning a collection of objects where an objects property matches any property from another collection of objects using LINQ-to-Entities
问题描述
我整天都在搜索,找不到解决方案...
I've been searching all day and can't find a solution to this...
我有一个EntityCollection
个Communication
个对象,每个对象都有一个Intention
个对象的实例(一对一).
I have an EntityCollection
of Communication
objects which each have an instance of an Intention
object(one-to-one).
我还有一个User
对象,该对象具有UserLocation
EntityObjects
(一对多)
I also have a User
object which has many instances of UserLocation
EntityObjects
(one-to-many)
-
Intention
对象具有属性UID
. -
UserLocation
对象具有属性LID
.
Intention
objects have a propertyUID
.UserLocation
objects have a propertyLID
.
我想编写一个LINQ表达式,该表达式返回所有Communication
对象,其中与Communication
对象关联的Intention
实例的UID
属性等于a的ANY实例的ANY LID
属性User
对象的UserLocation
实例.
I want to write a LINQ expression which returns all Communication
objects where the UID
property of the Intention
instance associated to a Communication
object equals ANY LID
property of ANY instance of a UserLocation
instance for a User
object.
我已经尝试过了
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语句中出现了一个错误,说委托System.Func<Communication,int,bool>
不带1个参数",不知道如何解决)
(this gives me an error on the Contains statement saying "Delegate System.Func<Communication,int,bool>
does not take 1 argument", don't know how to fix)
除了所有这些变化之外,我还拥有:
Along with all these variations I have also:
- 修改了我的方法以返回
IQueryable<Communication>
,并且在将ToList()
附加到查询中时也尝试了List<Communication>
.
- modified my method to return
IQueryable<Communication>
and have also triedList<Communication>
while appendingToList()
to my queries.
没有任何效果.不管我尝试什么,我总是会遇到这种例外情况
Nothing works. Regardless of what I try I always end up with this exception
NotSupportedException未通过用户代码处理
无法创建类型为'PreparisCore.BusinessEntities.UserLocation'的常量值.在这种情况下,仅支持基本类型(例如Int32,String和Guid).
我在做什么错??
推荐答案
给出以下代码:
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
我想念什么吗?
这篇关于使用LINQ-to-Entities返回对象集合,其中对象属性与另一个对象集合中的任何属性匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!