我正在尝试构建一个包含大量实体和一些服务操作的ADO.NET数据服务.一方面,我创建了一个ASP.NET Web应用程序,其中包含ADO.NET实体数据模型和ADO.NET数据服务.另一方面,我创建了第二个ASP.NET Web应用程序,它具有对数据服务的服务引用.
实体很顺利,我可以使用LINQ来检索我想要的数据:
TestEntities entities = new TestEntities(
new Uri("http://localhost/service/service.svc"));
var query = from customer in entities.Customers
where customer.ID == 1234
select customer;
query.ToList();
Run Code Online (Sandbox Code Playgroud)
这有效.但是,通过服务操作检索信息完全不适合我.数据服务端代码:
public static void InitializeService(IDataServiceConfiguration config) {
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
}
[WebInvoke]
public IQueryable<Customer> GetSomeCustomers() {
TestEntities entities = new TestEntities();
return from customer in entities.Customers
where customer.ID > 0 && customer.ID < 20
select customer;
}
Run Code Online (Sandbox Code Playgroud)
当我将服务引用添加到我的客户端项目时,Visual Studio没有接受任何服务操作.我知道我可以通过构造的URI和DataServiceContext对象或TestEntities对象的BeginExecute方法(在这种情况下)或类似的东西来访问它们,但这不是我想要的.
我想要的是使用LINQ来检查服务操作的返回数据.这可能吗?它应该是,对吧?
我有如下查询
var query = (from x in NetworkDevices
where
x.Name == "blabla1" ||
x.Name == "blabla2"
select x );
Run Code Online (Sandbox Code Playgroud)
我正在针对 Odata 端点运行它,因此它有效地被转换为以下 URL
https://targetserver/endpoint.svc/NetworkDevices()?$filter=Name eq 'blabla1' or Name eq 'blabla2'
Run Code Online (Sandbox Code Playgroud)
所以我想动态添加很多过滤器......在 C# 中,我可以继续将它添加到我的查询中,但这不是动态的。我想在运行时做。如果我从 Javascript 调用它,那么我也可以轻松地更新 URL。
我的问题是在 C# 中如何将这些过滤器动态添加到 where 子句中。
在普通的旧 LINQ(如 linq 2 对象)中,我可以做这样的事情。
var machines = new string[] { "blabla1" , "blabla2" } ;
res1.Where ( x => machines.Contains(x.Name) ).ToArray()
Run Code Online (Sandbox Code Playgroud)
这会起作用,但这对 Odata 端点不起作用,因为我收到这样的错误。
不支持方法“包含”
所以我认为唯一的方法是动态编辑表达式树或添加这些过滤器的东西。有人知道怎么做吗?
尝试使用主键CourseID针对odata web.api查找单个记录,方法如下:
var editedcourse = container.Courses.Where(c => c.CourseID == ID).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
这是错误的:
<m:innererror>
<m:message>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/atom+xml; charset=utf-8'.</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace></m:stacktrace>
<m:internalexception>
<m:message>'SingleResult`1' cannot be serialized using the ODataMediaTypeFormatter.</m:message>
<m:type>System.Runtime.Serialization.SerializationException</m:type>
Run Code Online (Sandbox Code Playgroud) wcf-data-services odata asp.net-web-api asp.net-web-api-odata
我建立了我在IIS 7中托管的WCFDataService,我将使用Reflection Provider作为数据源提供者.我的示例工作,如果我将实体类型定义保持在我定义服务的同一个程序集中,但是如果我将实体类型移动到另一个引用的程序集并且出现以下错误则不起作用
"服务器遇到处理请求的错误.异常消息是'在数据上下文类型'EntityContainer',有一个顶级IQueryable属性'Cats',其元素类型不是实体类型"
服务
public class WcfDataService1 : DataService<EntityContainer>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("Cats", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
Run Code Online (Sandbox Code Playgroud)
实体容器
public class EntityContainer
{
public IQueryable<Cat> Cats
{
get
{
var s = new List<Cat>();
var c1 = new Cat {Id = 1, Name = "Fufi"};
var c2 = new Cat {Id = 1, Name = "Felix"};
s.Add(c1);
s.Add(c2);
return s.AsQueryable();
}
}
}
Run Code Online (Sandbox Code Playgroud)
实体类型
[DataServiceKey("Id")]
public class Cat
{
public int Id { get; …Run Code Online (Sandbox Code Playgroud) 在另一篇文章中:Linq-To-Sql是否支持可组合查询,讨论了如何动态组合/连接where子句.这似乎是用"AND"完成的(即第一个where子句和第二个where子句由AND连接).我想知道的是,是否有办法用OR组合Linq查询.
例:
var people = from p in Person
where p.age < 18
select p
var otherPeople = from p in people
where p.firstName equals "Daniel"
select p
Run Code Online (Sandbox Code Playgroud)
这给人们起名为"丹尼尔"并且未满18岁.我正在寻找加入这些名称的语法,以找到名字为"丹尼尔"或未满18岁的人.
注意:我正在使用ADO.net数据服务,所以我没有.Contains()可供我使用.
编辑:联盟建议(由Garry Shutler)正是我正在寻找的功能方面.我确实遇到了两个可能的问题:
我有以下DataServiceQuery运行agaist和ADO数据服务(安装了更新以使其像.net 4一样运行):
DataServiceQuery<Account> q = (_gsc.Users
.Where(c => c.UserId == myId)
.SelectMany(c => c.ConsumerXref)
.Select(x => x.Account)
.Where(a => a.AccountName == "My Account" && a.IsActive)
.Select(a => a)) as DataServiceQuery<Account>;
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到一个例外:无法在单个资源上指定查询选项(orderby,where,take,skip)
据我所知,我需要使用一个包含附加lambda表达式的"SelectMany"版本(http://msdn.microsoft.com/en-us/library/bb549040.aspx),但我无法使其正常工作.
有人能告诉我如何正确构建"SelectMany"电话吗?
感谢您的任何帮助.
假设永远不会直接查询数据的情况.AKA,总会有一些必须发生的过滤逻辑和/或业务逻辑.
何时在ajax/js之外使用数据服务是一个很好的理由?
我开发了一个运行良好的WCF数据服务,我可以完成我想要的每个操作.
我开发了一个小型的c#客户端来测试所有功能:添加,删除,修改,检索数据.
在我的visual studio服务器上一切正常,但是一旦我在IIS服务器上:
当我尝试编辑数据时,我收到此错误:
System.Data.Services.Client.DataServiceRequestException未处理
Message = Une erreur s'est produite lors du traitement decetterequête.
Source = System.Data.Services.Client
StackTrace:
àSystem.Data.Services.Client.DataServiceContext.SaveResult.HandleBatchResponse()
àSystem.Data.Services.Client.DataServiceContext.SaveResult.EndRequest()
àSystem.Data.Services. Client.DataServiceContext.SaveChanges(SaveChangesOptions options) àSystem.Data.Services.Client.DataServiceContext.SaveChanges
()
àWSTester.Program.ModifySomeThings(Entities entities)dans D:\ Workspace\10067.GfK
Telecontrol.TOM\Release\V3. 1周\ WSTester \的Program.cs:LIGNE 90
àWSTester.Program.Main(字串[] args)丹斯d:\工作区\ 10067.GfK Telecontrol.TOM \推出\ V3.1\WSTester \的Program.cs:LIGNE 23个
à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)
àSystem.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)
àMicrosoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
àSystem.Threading.ThreadHelper .ThreadStart_Context(Object state)àSystem.Threading.ExecutionContext.Run
(ExecutionContext ex ecutionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)àSystem.Threading.ExecutionContext.Run
(ExecutionContext executionContext,ContextCallback callback,Object state)àSystem.Threading.ThreadHelper.ThreadStart
()
InnerException:
System.Data.Services.Client.DataServiceClientException
消息=严格// EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
401 - 未经授权:由于凭据无效,访问被拒绝.
.content-container {background:#FFF; …
我想创建一个从多个源返回数据的OData服务.出于这个原因,我不能将WCF数据服务与任何开箱即用的提供商一起使用,而且我们真的希望更多地控制数据模型.
如果我们坚持使用MS产品堆栈,我认为我们有两种选择;
将WCF与WebGet/WebInvoke一起使用以模仿OData api,并在内部执行查询字符串解析和转换.例如,对于我们的数据在Sql数据库中的情况,我们必须将$ filter子句转换为SQL where子句,以便构建我们的查询.请注意,我们在这里不能使用任何类型的ORM,因为我们的数据模型是动态的,并且我们没有任何可以用ORM填充的CLR实体类.
我们将WCF数据服务与自定义提供程序一起使用,这要求我们为资源集传递IQueryable,这使我们可以执行Select*FROM Table并使用Linq对象,或者实现我们自己的IQueryable提供程序,该提供程序支持所需的表达式由OData.WCF数据服务甚至会接受IQuerable吗?
哪个最容易实现?我们大多只想支持OData规范的$ filter功能,$ expand和$ select可以在以后使用.
放弃WCF数据服务似乎是一种耻辱,如果它可以提供解析后的OData查询,然后您可以自己转换为Linq查询,而不是期望您的数据源具有IQueryable提供程序,那将更为可取.