LINQ to SQL - 搜索连接表的列

发布于 2024-12-09 03:12:43 字数 1526 浏览 6 评论 0原文

我试图返回一个结果集,其中包含三个字符串中任何一个都具有字符串匹配的行。我的域模型如下所示:

public class Customers
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class BidNames
{
    public int BidNameID { get; set; }
    public int CustomerID { get; set; }
    public string BidName { get; set; }
}

BidName.CustomerID 和 Customers.CustomerID 之间存在 FK,并且它是一对多关系,其中单个客户可以有多个 BidName。当搜索我的客户表时,我想返回名字、姓氏或任何关联的 BidNames 中存在字符串匹配的所有记录。在本例中,我将使用 BidName 搜索字符串“ba”。

from c in Customers
where c.FirstName.Contains("ba") || c.LastName.Contains("ba") || c.BidNames.Any(b=>BidNames.BidName.Contains("ba"))
orderby c.LastName, c.FirstName
select new { CustomerID = c.CustomerID, FirstName = c.FirstName, LastName = c.LastName }

这一切都有效,直到我在Where 子句中添加最终标准。我知道 c.BidNames 是一个集合,我想看看它们中是否有一个包含“ba”的 BidName。我遇到麻烦的地方是尝试指定 BidNames.BidName 列来搜索字符串。我上面编写的代码失败,并显示“BidNames 不包含“BidName”的定义”。

如何编写Where 子句的最后一部分,以便可以搜索与客户记录关联的所有 BidNames.BidName 字段?我希望并假设我也可以使用相同的语法在 orderby 和 select 子句中指定 BidName 字段。

非常感谢,

BK

最终答案:

from c in Customers
where 
    c.FirstName.Contains("ba") || 
    c.LastName.Contains("ba") || 
    c.BidNames.Any(b=>b.BidName.Contains("ba"))
orderby 
    c.LastName, 
    c.FirstName
select new { 
    CustomerID = c.CustomerID, 
    FirstName = c.FirstName, 
    LastName = c.LastName,
    BidNames = c.BidNames.OrderBy(b=>b.BidName)
    }

I am trying to return a result set that includes rows where any of three strings has a string match. My domain models look like this:

public class Customers
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class BidNames
{
    public int BidNameID { get; set; }
    public int CustomerID { get; set; }
    public string BidName { get; set; }
}

There is a FK between BidName.CustomerID and Customers.CustomerID and it is a one-to-many relationship where there can be multiple BidNames for a single Customer. When searching my customer table I want to bring back all records where there is a string match in FirstName, LastName or any of the associated BidNames. For this example I'll use a BidName search string of "ba".

from c in Customers
where c.FirstName.Contains("ba") || c.LastName.Contains("ba") || c.BidNames.Any(b=>BidNames.BidName.Contains("ba"))
orderby c.LastName, c.FirstName
select new { CustomerID = c.CustomerID, FirstName = c.FirstName, LastName = c.LastName }

It all works until I add the final criteria in the Where clause. I understand that c.BidNames is a collection and I'm looking to see if Any of them have a BidName that contains "ba". Where I'm running into trouble is trying to specify the BidNames.BidName column to search for the string. The code I've written above fails with "BidNames does not contain a definition for 'BidName'"

How do I write the last part of the Where clause so I can search all the BidNames.BidName fields associated with the Customer record? I hope and assume I can use the same syntax to specify the BidName field in the orderby and select clauses as well.

Many thanks,

BK

FINAL ANSWER:

from c in Customers
where 
    c.FirstName.Contains("ba") || 
    c.LastName.Contains("ba") || 
    c.BidNames.Any(b=>b.BidName.Contains("ba"))
orderby 
    c.LastName, 
    c.FirstName
select new { 
    CustomerID = c.CustomerID, 
    FirstName = c.FirstName, 
    LastName = c.LastName,
    BidNames = c.BidNames.OrderBy(b=>b.BidName)
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-12-16 03:12:43

尝试将 where 子句更改为

Where c.FirstName.Contains("ba") || 
      c.LastName.Contains("ba") || 
      c.BidNames.Any(b=>b.BidName.Contains("ba"))

在 Any 子句中,b 是 BidNames 类的实例,因此您希望访问该类的属性而不是 Customer 的 BidNames 属性。

Try changing your where clause to

Where c.FirstName.Contains("ba") || 
      c.LastName.Contains("ba") || 
      c.BidNames.Any(b=>b.BidName.Contains("ba"))

In your Any clause, b is an instance of the BidNames class, so you want to access properties of that rather than the BidNames property of the Customer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文