【问题标题】:C# Linq Join Lambda (a key selector contains the other key selector)C# Linq Join Lambda(一个键选择器包含另一个键选择器)
【发布时间】:2015-10-26 22:48:12
【问题描述】:

类似于 sql 查询“where a like '%b%'”

.Net framework 4.6 的 C# 不接受这个:

context.Table1
    .Join(
        context.Table2,
        table1 => table1.strStringContainsIntegers, // string (.net needs int, 
                                  //I need string that contains list of integers)
        table2 => table2.intInteger,                // integer
        (table1, table2) => new { table1.SomeField, table2.SomeField }
    ).Where(o=>o.table1.strStringContainsIntegers.Contains(table2.intInteger+"-"));

如何将一个列上的两个表/列表(键选择器 2)包含在另一列(键选择器 1)中?

【问题讨论】:

    标签: c# linq join key


    【解决方案1】:

    你可以这样做:

    var query =
        context
            .Table1
            .SelectMany(table1 =>
                context
                    .Table2
                    .Where(table2 => table1.strStringContainsIntegers.Contains(table2.intInteger.ToString()))
                    .Select(
                        table2 => new {table1.SomeField, table2.SomeField}));
    

    LINQ 会将其转换为 SQL 连接。

    【讨论】:

      猜你喜欢
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多