使用 ADO.Net 数据服务和 LINQ 选择单一值
尝试 ADO.Net 数据服务。 所有示例都展示了如何检索列表,但您将如何检索单个值? 例如产品 X 的价格。
这是我使用的 LINQ 查询:
var qry =(来自 p 中 svcContext.产品 其中 p.ProductName == “椅子” && p.颜色 == 1 选择c)作为DataServiceQuery;
产品退回Prod;
qry.BeginExecute( (pr)=> returnedProd = qry.EndExecute(pr).First(), null);
在这里,我尝试检索产品并将其加载到本地变量中,但本地变量保持为空。
很确定,我做的完全错误:)...任何帮助将不胜感激。
Trying my hand at ADO.Net data services. All the examples shows how to retrieve lists but how would you go about retrieving a single value? e.g. Product X's Price.
Here is the LINQ query i use:
var qry = (from p in
svcContext.Products
where p.ProductName == "Chair"
&& p.Colour == 1
select c) as DataServiceQuery;Product returnedProd;
qry.BeginExecute(
(pr) => returnedProd = qry.EndExecute(pr).First(), null);
Here i try to retrieve the product and load it into a local variable, but the local var stays null.
Pretty sure, i'm doing it completely wrong :)...any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不应该是
var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select p) 你在哪里声明了 c ?
It's not suppose to be
var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select p) where did you declare the c ?
抱歉本来应该是
Sorry was supposed to be
如果结果集为空,
First()
应该抛出异常 - 您确定查询正在执行吗?First()
should throw an exception if the result set is empty - are you sure the query is even executing?您并不是第一个受到所有 silverlight 传出请求的异步特性影响的人。
在 lambda 表达式中,
您捕获局部变量 returnedProd,但通常在调用 BeginExecute 之后将分拆的线程为时已晚。 它可能会在执行超出当前方法的范围后执行,并且变量将丢失。
解决方案是有效地使用“returnedProd”来填充 UI 或执行 IN lambda 表达式所需的任何操作。 比如:
否则对社区有用的答案,我希望几周前就有一个:(
You are not the first to get hit by the asynchronous nature of all silverlight outgoing requests.
In the lambda expression
you capture the local variable returnedProd but usually the thread that will spin off AFTER BeginExecute has been called will be too late. It will probably executed after the execution goes out of scope of the current method and the variable will be lost.
The solution is to use effectively the "returnedProd" to populate the UI or whatever you need to do IN the lambda expression. Something like :
Otherwise useful answer for the community, I wish I had one a few weeks ago :(