简体   繁体   English

LINQ谓词在C#

[英]linq predicate in c#

i have two tables(for Ex:TableEmployee,TableSal) with no relationship, and i have to write predicate using these two tables, 我有两个没有关系的表(例如Ex:TableEmployee,TableSal),我必须使用这两个表编写谓词,

TableEmployees:This table contains Name, DepartmentId columns TableEmployees:此表包含“名称”,“部门编号”列

TableSal: this table contains Name,DepartmentId,TotalSal TableSal:此表包含Name,DepartmentId,TotalSal

Question: i have to write predicate for "is DepartmentId in TableSal Matches with any DepartmentId in TableEmployees" 问题:我必须为“ TableSal中的DepartmentId与TableEmployees中的任何DepartmentId匹配是否要写谓词”

i've tried these methods but getting erors, 我尝试过这些方法,但会出错,

Method1: 方法1:

predicate = predicate.And(
      n => context.Tbl_Sal
        .Where(d => d.DepartmentId.HasValue)
        .Select(i => i.DepartmentId)
        .Equals(n.DepartmentId)
      );

Method2: 方法2:

predicate = predicate.And(
   n => context.Tbl_Sal
      .Where(d => d.DepartmentId.HasValue)
      .ToList().Find(d => d.DepartmentId == n.DepartmentId) != null);

How can i write Predicate.... for this situation.... 对于这种情况,我该如何写谓词。

I wonder why you're dealing with raw expressions. 我想知道为什么您要处理原始表达式。 Using linq, you could do: 使用linq,您可以执行以下操作:

var query = context.TableSal
           .Where(d => context.TableEmployees.Where(e => e.DepartmentId.HasValue)
                      .Select(e => e.DepartmentId)
                      .Contains(d.DepartmentId));

which gives you all TableSal records of which DepartmentId is in the DepartmentId s of TableEmployees . 这将为您提供所有TableSal记录,其中DepartmentIdTableEmployeesDepartmentId中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM