我正在尝试使用Silverlight ADO.Net数据服务客户端api(以及Linq To Entities)创建一个使用where子句中的id列表的查询.有没有人知道Contains不受支持的解决方法?
我想做这样的事情:
List<long?> txnIds = new List<long?>();
// Fill list
var q = from t in svc.OpenTransaction
where txnIds.Contains(t.OpenTransactionId)
select t;
Run Code Online (Sandbox Code Playgroud)
试过这个:
var q = from t in svc.OpenTransaction
where txnIds.Any<long>(tt => tt == t.OpenTransactionId)
select t;
Run Code Online (Sandbox Code Playgroud)
但得到"方法'任何'不受支持".
为什么我会收到错误:
无法创建"闭包类型"类型的常量值.在此上下文中仅支持基本类型(例如Int32,String和Guid).
当我尝试枚举以下Linq查询?
IEnumerable<string> searchList = GetSearchList();
using (HREntities entities = new HREntities())
{
var myList = from person in entities.vSearchPeople
where upperSearchList.All( (person.FirstName + person.LastName) .Contains).ToList();
}
Run Code Online (Sandbox Code Playgroud)
更新:如果我尝试以下尝试隔离问题,我得到相同的错误:
where upperSearchList.All(arg => arg == arg)
Run Code Online (Sandbox Code Playgroud)
所以看起来问题出在All方法上,对吧?有什么建议?
我有一个名为UserTenders与aspnet_Membership表有多对一关系的表.
我正在使用EntityFramework 4.0,当我尝试这样的东西时,它会出错.
var tenders = ctx.UserTenders
.Where(tender => tender.HasAdminApproved.Equals(true))
.ToList();
Run Code Online (Sandbox Code Playgroud)
错误是
System.NotSupportedException
无法创建类型为"System.Object"的常量值.
在此上下文中仅支持原始类型(例如Int32,String和Guid').
下面的代码片段有效.
var tenders = ctx.UserTenders.ToList();
Run Code Online (Sandbox Code Playgroud)
我的代码可能有什么问题?觉得我错过了一些非常微不足道的事情.
我想过滤所有那些行bit场HasAdminApproved为true
我收到以下错误:
无法创建类型为"Phoenix.Intranet.Web.ClientSettings.ComponentRole"的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').
我理解为什么会出错.我不明白的是我的代码创建错误的原因.我的比较是针对原始类型的.所有的比较都是Guid to Guid.该错误明确指出Guids是可以的.
此行发生错误(朝向底部):
var vla = (from cir in phoenixEntities.ComponentInRoles
Run Code Online (Sandbox Code Playgroud)
码:
List<ComponentRole> roles;
using (IMSMembershipEntities entities = new IMSMembershipEntities())
{
roles = (from role1 in entities.Roles
select new ComponentRole{Name = role1.RoleName, RoleId = role1.RoleId} ).ToList();
}
List<Components> componentInRoles;
using (PhoenixEntities phoenixEntities = new PhoenixEntities())
{
phoenixEntities.ContextOptions.LazyLoadingEnabled = false;
componentInRoles = (from component in phoenixEntities.Components
select new Components{Name = component.Name,
ComponentId = component.ComponentId,
//InRoles = (from componentInRole in phoenixEntities.ComponentInRoles
// join role in roles on componentInRole.RoleId equals role.RoleId …Run Code Online (Sandbox Code Playgroud) 要选择所有Scheduling活动的,我有以下代码:
var allSchedulesOnALine = CurrentUser.Lines.SelectMany(o => o.Scheduling).Where(o => o.Active);
var allSchedulesUnscheduled = Entities.SchedulingSet
.Where(o => o.Line == null && o.Site.Id == CurrentUser.Site.Id &&
o.Factory == CurrentUser.Factory && o.Active);
IEnumerable<Scheduling> allSchedules = allSchedulesUnscheduled.Union(allSchedulesOnALine);
foreach(Scheduling schedule in allSchedules.OrderBy(o => o.Ordering))
{
//Do Stuff
}
Run Code Online (Sandbox Code Playgroud)
(Factory是int)
当我运行此代码时,我得到了这个神秘的错误foreach:
无法创建类型为'System.Collections.Generic.IEnumerable`1'的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').
奇怪的是,我可以枚举都allSchedulesOnALine和allSchedulesUnscheduled独立.更奇怪的是,如果我重新订购联盟:
IEnumerable<Scheduling> allSchedules = allSchedulesOnALine.Union(allSchedulesUnscheduled);
Run Code Online (Sandbox Code Playgroud)
它工作正常!
有谁知道为什么会这样?我错过了一些关键的东西,还是这个错误?
我应该提到我正在使用Entity Framework 3.5.EF4目前不是我们的选择 - 它超出了我的控制范围:\
如何在EF中编写这样的子查询?
select * from table1 where col1 in (select col1 from table2 where col2 = 'xyz')
Run Code Online (Sandbox Code Playgroud)
要么
select * from table1 where col1 not in (select col1 from table2 where col2 = 'xyz')
Run Code Online (Sandbox Code Playgroud)
我试过这样的东西
from t1 in table1
where (from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1)
select t1
Run Code Online (Sandbox Code Playgroud)
和
from t1 in table1
where !(from t2 in table2 where col2 = 'xyz' select t2.col1).Contains(t1.col1)
select t1
Run Code Online (Sandbox Code Playgroud)
这些查询工作正常LinqPad或Linq到Sql