c# - Linq 不区分大小写的连接

标签 c# linq join case-insensitive

我想实现两件事。

首先,我希望此连接不区分大小写。

我以前使用过这种不区分大小写的 where 子句

where b.foo.Equals(foo, StringComparison.OrdinalIgnoreCase)

但我现在不知道如何在 join 中使用它。

其次,我想返回包含作者姓名和书籍数量的元组。

        var query = from b in Books
                    join a in authors on b.Author equals a
                    select Tuple.Create(a, _count_of_authors_books_);

        return query;

谢谢。

最佳答案

Linq 确实支持使用不区分大小写的匹配进行连接,但只是在查询语法中不支持。您需要使用 Method Syntax .

var query = Books.Join(
    authors, // the other list
    book => book.Author, // what to compare in "Books"
    author => author, // what to compare in "authors"
    (book, author) => Tuple.Create(author, _count_of_authors_books_), // what to select at the end
    StringComparer.InvariantCultureIgnoreCase); // how to do the comparison

StringComparer有一些其他变体,请使用您需要的变体。

关于c# - Linq 不区分大小写的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29612122/

相关文章:

c# - DataGridViewComboBoxColumn 数据源?

c# - 如何检索到可查询成员的绑定(bind)?

c# - 将委托(delegate)传递给 Sort()

C# Linq GroupBy ==> 类 vs 匿名类 --- 不同的结果

php - 如何订购这个特定的内部联接?

python - Pyspark 对列中列表中的值进行联接和操作

MySQL 连接,查询未结束(循环?)

c# - C#中如何从字符串中获取数字

c# - 菜单屏幕/屏幕更改在 C# 和 XNA 中如何工作?

c# - 如何让 Silverlight 动画/应用程序在 WPF 中也能正常工作/相同?