简体   繁体   English

与LINQ C#中的多个Where子句比较

[英]Comparison with multiple Where clause in LINQ C#

I have two DataTables: 我有两个数据表:

DataTable dtCatalogFromMySql;
DataTable dtDataForExistingProducts;

dtCatalogFromMySql dtCatalogFromMySql

Id | productname  | barcode  | pricesell| type  
---+--------------+----------+----------+------
1  | Abz          | 123      | 2.01     | RS // different product name
2  | Abd          | 122      | 8.90     | RS // different price
3  | Abx          | 125      | 21.00    | WS  // both different
4  | Abf          | 124      | 2.11     | RS
5  | Abg          | 126      | 8.01     | WS 
6  | Abh          | 127      | 60.23    | RS
7  | Abi          | 128      | 9.10     | RS

dtDataForExistingProducts dtDataForExistingProducts

Id | productname  | barcode  | pricesell| type  
---+--------------+----------+----------+------
1  | Abc          | 123      | 2.01     | RS
2  | Abd          | 122      | 3.90     | RS
3  | Abe          | 125      | 23.00    | WS 
4  | Abf          | 124      | 2.11     | RS
5  | Abg          | 126      | 8.01     | WS 
6  | Abh          | 127      | 60.23    | RS
7  | Abi          | 128      | 9.10     | RS

I need return only rows which are different as in first table 我只需要返回与第一个表不同的行

I need select all data where Prod_No equals to baracode and Descript not equals to productname and Retail_PRC also not equals to pricesell . 我需要选择Prod_No 等于 baracodeDescript 不等于 productnameRetail_PRC 也不等于 pricesell所有数据。

I am not getting results with this code 我没有得到此代码的结果

List<DataRow> matchingRows = dtCatalogFromMySql.AsEnumerable()
    .Where(a => dtDataForExistingProducts.AsEnumerable()
        .Select(b => b.Field<string>("Prod_No"))  
        .Contains(a.Field<string>("barcode")))
    .Where(a => !dtDataForExistingProducts.AsEnumerable()
        .Select(b => b.Field<string>("Descript"))
        .Equals(a.Field<string>("productname")))
    .Where(a => !dtDataForExistingProducts.AsEnumerable()
        .Select(b => b.Field<decimal>("Retail_PRC"))
        .Equals(Convert.ToDecimal(a.Field<double>("pricesell"))))
    .ToList();

I suppose, Contains() will also fetch the data if barcode = 123456 and Prod_No = 1234 , it is right? 我想,如果barcode = 123456Prod_No = 1234Contains()也将获取数据,对吗? If I am right what is right way to compare string exactly same 如果我是对的,那么比较字符串完全相同的正确方法是什么

You may want to consider a clearer syntax such as: 您可能需要考虑更清晰的语法,例如:

var results = from t1 in dtCatalogFromMySql.AsEnumerable()
              join t2 in dtDataForExistingProducts.AsEnumerable() on 
                  (string)t1["barcode"] equals (string)t2["Prod_No"]
              where (string)t1["productname"] != (string)t2["descript"] &&
                    Convert.ToDecimal((double)t1["pricesell"]) != 
                    (decimal)t2["Retail_PRC"]
              select t2;

The Join is definitely the way to go. 加入绝对是必经之路。 You can modify the select according to your required result set. 您可以根据所需的结果集修改选择。

trighati makes a good point about using OR instead of AND . trighati很好地说明了使用OR代替AND This is assuming that you want all of the data where at least one of your values changed where Prod_no and barcode are equal. 这是假设您要在Prod_no和条形码相等的情况下,至少要更改一个值的所有数据。 This would change the query to be: 这会将查询更改为:

var results = from t1 in dtCatalogFromMySql.AsEnumerable()
              join t2 in dtDataForExistingProducts.AsEnumerable() on
                  (string)t1["barcode"] equals (string)t2["Prod_No"]
              where (string)t1["productname"] != (string)t2["descript"] ||
                    Convert.ToDecimal((double)t1["pricesell"]) != 
                    (decimal)t2["Retail_PRC"]
              select t2;

Use Join to combine them into one result set, then filter the result set: 使用Join将它们组合成一个结果集,然后过滤结果集:

var combined = dtDataForExistingProducts.AsEnumerable()
    .Join(dtCatalogFromMySql.AsEnumerable(), 
        ep => ep.Field<string>("Prod_No")
        ce => ce.Field<string>("barcode"), 
        (ep, ce) => new {ExistingProduct = ep, CatalogEntry = ce})
    .Where(m => !m.ExistingProduct.Field("Descript")
        .Equals(m.CatalogEntry.Field("productname")))
    .Where(m => decimal.Parse(m.ExistingProduct.Field("Retail_PRC").ToString()) 
        != decimal.Parse(m.CatalogEntry.Field("pricesell").ToString()))
    .ToList()
;

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

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