我能使用让LINQ加入 - C# [英] Can i use join with let in linq - c#
本文介绍了我能使用让LINQ加入 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
let order= _relationContext.Orders
.Where(x => x.OrderNumber == orderNo)
.Select(x => new { x.OrderNo, x.OrderDate }).Single()
我想尝试做这样的事情
let order = _relationContext.Orders join _relationContext.Products
.Where(x => x.OrderNumber == orderNo && x.ProductId == Products.ProductID)
.Select(x => new { x.OrderNo, x.OrderDate }).Single()
这甚至可能
更新1:
我当前的代码
UPDATE 1: My current code
var q = from c in sXDocument.Descendants("prop")
let handle = c.Element("handle")
let resultref = handle != null ? handle.Element("dsref") : null
let orderno = (string)c.Element("orderno")
let orderdetail = _relationContext.Order
.Where(x => x.orderno == orderno)
.Select(x => new { x.ProductID, x.OrderDate }).Single()
select new Order()
{
OrderNo = orderno,
Handle = resultref != null ? (string)resultref.Attribute("handle") : null,
Title = //Need ProductName,
ProductID = orderdetail.ProductID.ToString(),
};
return q.ToList();
我想,如果我可以使用let关键字的联接,这样我就能拿到产品名称
I figured that if I could use a join in let keyword so I could get the product name
推荐答案
您只能用让作为查询表达式的一部分。如果你要定义一个独立的变量,只是声明它以正常的方式。如果你可以给我们你想要做什么,这将真正帮助更多的什么主意。
You can only use "let" as part of a query expression. If you want to define a separate variable, just declare it in the normal way. If you could give us more idea of what you're trying to do, that would really help.
编辑:您的可以的使用加盟内的让的条款,但只有当它里面一个完整的查询表达式:
You can use "join" within a "let" clause, but only if it's a full query expression inside:
from foo in foos
let bar = (from x in baz join y in qux on x.Id equals y.Id select ...)
select ...
你确定你不应该使用加入让利,而不是开始吗?
Are you sure you shouldn't be using a join instead of a let to start with?
这篇关于我能使用让LINQ加入 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文