C# 从sql转换为linq查询
我只是想将下面的查询转换为C# 从sql转换为linq查询,c#,sql,linq,C#,Sql,Linq,我只是想将下面的查询转换为LINQ表达式 Select * from customer where customer_id in (select customer_id from cust where username=name) 假设您可以访问dbcontext.customer中的customer表和dbcontext.cust中的cust表 var name = "desired username" var result = dbcontext.customer.Where(
LINQ
表达式
Select *
from customer
where customer_id in
(select customer_id from cust where username=name)
假设您可以访问
dbcontext.customer中的customer表和dbcontext.cust中的cust表
var name = "desired username"
var result = dbcontext.customer.Where(customer => dbcontext.cust.Where(cust => cust.username == name).Select(cust => cust.customer_id).Any(cust => cust == customer.customer_id)).ToList();
假设您可以访问dbcontext.customer中的customer表和dbcontext.cust中的cust表
var name = "desired username"
var result = dbcontext.customer.Where(customer => dbcontext.cust.Where(cust => cust.username == name).Select(cust => cust.customer_id).Any(cust => cust == customer.customer_id)).ToList();
因此您有两个表:一个Cust
表和一个Customer
表。两个表都有一个属性CustomerId
。Cust
表还有一个属性UserName
现在给定一个字符串变量name
,您希望Customers
表中所有元素的CustomerId
等于Cust
表中所有元素的CustomerId
,这些元素的用户名
等于name
对我来说,似乎是一种加入:
var result = myDbContext.Cust // take the cust table
.Where(cust => cust.UserName == name) // keep only custs with Username == name
.Join(myDbContext.Customers, // Join the result with Customers table
cust => cust.CustomerId, // From every cust take the CustomerId
customer => customer.CustomerId, // From every customer take the CustomerId
(cust, customer) => // when they match take the cust and the Customer
customer // to select the matching Customer
因此您有两个表:一个Cust
表和一个Customer
表。两个表都有一个属性CustomerId
。Cust
表还有一个属性UserName
现在给定一个字符串变量name
,您希望Customers
表中所有元素的CustomerId
等于Cust
表中所有元素的CustomerId
,这些元素的用户名
等于name
对我来说,似乎是一种加入:
var result = myDbContext.Cust // take the cust table
.Where(cust => cust.UserName == name) // keep only custs with Username == name
.Join(myDbContext.Customers, // Join the result with Customers table
cust => cust.CustomerId, // From every cust take the CustomerId
customer => customer.CustomerId, // From every customer take the CustomerId
(cust, customer) => // when they match take the cust and the Customer
customer // to select the matching Customer
请试试这个
var custList = ctx.Customer.Where(x=>x.username==name).ToList();
var finalList=ctx.Customer.ToList().Where(x=>custList.Select(x=>x.customer_id).ToList().Contains(x.customer_id))
请试试这个
var custList = ctx.Customer.Where(x=>x.username==name).ToList();
var finalList=ctx.Customer.ToList().Where(x=>custList.Select(x=>x.customer_id).ToList().Contains(x.customer_id))
您尝试了什么?您使用了NHibernate、实体框架、数据集等哪些部分?您在开发的哪个阶段,您有ORM,或者您甚至使用了数据库,您是否有任何工作,或者这只是一个假设问题从cust表获取客户ID(IEnumerable CustomerID=cust.Where(x=>x.username==name)
)然后customer.Where(x=>customerIDs.Contains(x.customer\u id))
应该转换为IN子句。但是为什么有两个不同的客户表?您尝试了什么?您坚持使用哪一部分?您使用的是NHibernate、实体框架、数据集等吗?您处于开发的哪个阶段,您是否有ORM,或者您甚至使用了数据库,您是否有任何工作,或者这只是一个假设问题cust表中的customer\u id(IEnumerable customerIDs=cust.Where(x=>x.username==name)
),然后是customer.Where(x=>customerIDs.Contains(x.customer\u id))
应该转换为IN子句。但是为什么您有两个不同的客户表?谢谢。我将在我的应用程序中尝试它。它不起作用,因为表在不同的上下文中。@Dip如果它们在不同的上下文中没有区别,您可以用包括其他con在内的任何其他集合替换dbcontext.cust
text.谢谢。我将在我的应用程序中试用它。它不起作用,因为表在不同的上下文中。@如果它们在不同的上下文中,则没有区别,您可以用任何其他集合(包括其他上下文)替换dbcontext.cust
。