gpt4 book ai didi

LINQ 加入多个条件?

转载 作者:行者123 更新时间:2023-12-02 02:08:59 26 4
gpt4 key购买 nike

我有以下有效的 SQL 查询:

select a.Id, a.Name
from Addresses a join Companies c
on c.AddressId = a.Id
or c.MailAddressId = a.Id
or c.Id = a.CompanyId
where c.Id = *CompanyId* and a.Name = *AddressName*

检查具有所提供地址名称的地址是否链接到具有所提供公司 ID 的公司。

如何在 LINQ for EF 中表达这一点?

更新:Address 和 Company 类如下(仅包含与此问题相关的详细信息):

public class Address
{
public int Id {get; set;}
public string Name {get; set;}
public int? CompanyId {get; set;}
public virtual Company {get; set;}
}
public class Company
{
public int Id {get; set;}
public string Name {get; set;}

public int AddressId {get; set;}
public virtual Address Address {get; set;}

public int? MailAddressId {get; set;}
public virtual Address MailAddress {get; set;}

public virtual ICollection<Address> DeliveryAddresses {get; set;}
}

谢谢。

最佳答案

LINQ 仅支持相等联接。在其他情况下,您应该使用交叉连接,其中:

from a in Addresses
from c in Companies
where (c.AddressId == a.Id || c.MailAddressId == a.Id || c.Id == a.CompanyId)
&& (c.Id == *CompanyId* && a.Name == *AddressName*)
select new{a.Id, a.Name}

关于LINQ 加入多个条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13741631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com