如何使用C#Expression API在Lamba中使对象列表成为参数

时间:2019-07-12 17:41:07

标签: c# expression

我正在尝试动态创建一个谓词以传递给linq where子句。这是一种通用方法,该方法需要两个具有相同Type的列表和一个属性名列表进行比较。

void SomeMethod<T>(List<T> oldRecords, List<T> newRecords, List<string> propertiesOfT)
{
    // dynamically build predicate for this
    var notMatch = oldRecords.Where(o => !newRecords.Any(n => n.Prop1 == o.Prop1 && n.Prop2 == o.Prop2)).ToList();

    // do somethind with notMatch

}

我想将其转换为:

var notMatch = oldRecords.Where(o => !newRecords.Any(n => n.Prop1 == o.Prop1 && n.Prop2 == o.Prop2)).ToList();

要实现这一目标:

var predicate = "n => n.Prop1 == o.Prop1 && n.Prop2 == o.Prop2"; // sudo code
var notMatch = oldRecords.Where(o => !newRecords.Any(predicate));

或这个

var predicate = "o => !newRecords.Any(n => n.Prop1 == o.Prop1 && n.Prop2 == o.Prop2)" // sudo code
var notMatch = oldRecords.Where(predicate);

动态创建表达式时如何填充newRecords? 以及如何在Expression中引用参数o和参数n。

我已经走了这么远:

//construct the two parameters
var o = Expression.Parameter(typeof(T), "o");
var n = Expression.Parameter(typeof(T), "n");

// How to I go about populating o with values and n with values
// from oldRecords and newRecords? or is that no neccessary

var property = Expression.Property(o, typeof(T).GetProperty("Id").Name);

var value = Expression.Constant(Convert.ChangeType("12345", typeof(T).GetProperty("Id").PropertyType), typeof(T).GetProperty("Id").PropertyType);

BinaryExpression binaryExpression = Expression.MakeBinary(ExpressionType.Equal, property, value);

任何sudo代码或在哪里寻找实现此目标的线索?

1 个答案:

答案 0 :(得分:1)

有了反思,这很容易。您只需要考虑一下。这是工作版本。

CREATE TABLE location (    
  zipcode int CONSTRAINT zipcode_pk PRIMARY KEY,    
  city varchar2 (50)      
)
;

CREATE TABLE employees (    
   employeeid int CONSTRAINT employeeid_pk PRIMARY KEY,    
   firstname varchar2 (50),    
   lastname varchar2 (50),    
   street varchar2 (50),    
   state varchar2 (2),    
   zipcode int REFERENCES location (zipcode),    
   datehired date,    
   phonenum int,    
   salaryhr number       
)
;

CREATE TABLE customer (    
   customerponum int CONSTRAINT customerponum_pk PRIMARY KEY,    
   firstname varchar2 (50),    
   lastname varchar2 (50),    
   street varchar2 (50),    
   zipcode int REFERENCES location (zipcode)    
)
;

CREATE TABLE products (    
   productid int CONSTRAINT productid_pk PRIMARY KEY,    
   productname varchar2 (50),    
   price number (*,2),    
   costpercent int   
)
;

CREATE TABLE inventory (  
   productid int REFERENCES products (productid),  
   unitonhand int,  
  CONSTRAINT productid_pkt PRIMARY KEY (productid)  
)
;

CREATE TABLE sales (     
   ordernum int CONSTRAINT ordernum_pk PRIMARY KEY,     
   customerponum int REFERENCES customer (customerponum),     
   productid int REFERENCES products (productid),     
   employeeid int REFERENCES employees (employeeid),     
   saledate date,     
   unitssold int     
)
;

INSERT ALL  
  INTO  location (zipcode, city) VALUES (77095, 'Houston')  
  INTO  location (zipcode, city) VALUES (77451, 'Dallas')  
  INTO  location (zipcode, city) VALUES (77114, 'Austin')  
  INTO  location (zipcode, city) VALUES (77369, 'Lubbock')  
  INTO  location (zipcode, city) VALUES (75451, 'El Paso')  
SELECT * FROM dual
;

INSERT ALL  
  INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (101, 'Josh', 'Smith', '100 Baker St',77095)  
  INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (102, 'John', 'Doe', '12 Yankee Ave',77451)  
  INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (103, 'Brandon', 'Markle', '1 Longhorn Blvd',77114)  
  INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (104, 'Mary', 'Eglin', '223 Aggie St',77369)  
  INTO customer (customerponum, firstname, lastname, street, zipcode) VALUES (105, 'Sue', 'Fields', '91 Patriot',75451)  
SELECT * FROM dual
;
'''

---Oracle liveSQL this is the statement that I receive the error
---

INSERT ALL  
  INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (1, 'Jason', 'Wayne', '103 Brown St', 'TX', 77453, '14-may-13', 2814441304, 13)  
  INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (2, 'Jacob', 'Dutch', '14 Yawn Rd', 'TX', 77096, '12-july-11', 8325472222, 10)  
  INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (3, 'Susan', 'Anthony', '1 Patronas Ln', 'TX', 77231, '08-jan-17', 2819993547, 9)  
  INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (4, 'David', 'Lane', '888 Madrid Blvd', 'TX', 78113, '27-dec-18', 8321119876, 8)  
  INTO employees (employeeid, firstname, lastname, street, state, zipcode, datehired, phonenum, salaryhr) VALUES (5, 'Anthony', 'Barnard', '21 Adiom Cl', 'TX', 79448, '13-nov-17', 2814558008, 10)  
SELECT * FROM dual
;

这是我尝试过的一个样本集,它可以工作

ORA-02291: integrity constraint (SQL_NCBYEZZVAYRPDIJSWAZMSKRHK.SYS_C0016383126) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1721
相关问题