简体   繁体   English

使用C#和LINQ进行左右/内部联接

[英]Left/Right/Inner joins using C# and LINQ

I am trying to figure out how to do a series of queries to get the updates, deletes and inserts segregated into their own calls. 我试图弄清楚如何执行一系列查询以获取隔离在其自身调用中的更新,删除和插入。 I have 2 tables, one in each of 2 databases. 我有2个表,每个2个数据库中有一个。 One is a Read Only feeds database and the other is the T-SQL R/W Production source. 一个是只读提要数据库,另一个是T-SQL R / W生产源。 There are a few key columns in common between the two. 有一些常用两者之间的几个关键列。

What I am doing to setup is this: 我要设置的是这样的:

List<model.AutoWithImage> feedProductList = _dbFeed.AutoWithImage.Where(a => a.ClientID == ClientID).ToList();
List<model.vwCompanyDetails> companyDetailList = _dbRiv.vwCompanyDetails.Where(a => a.ClientID == ClientID).ToList();
foreach (model.vwCompanyDetails companyDetail in companyDetailList)
{
    List<model.Product> productList = _dbRiv.Product.Include("Company").Where(a => a.Company.CompanyId == companyDetail.CompanyId).ToList();
}

Now that I have a (source) list of products from the feed, and an existing (target) list of products from my prod DB I'd like to do 3 things: 现在,我有一个来自提要的产品(源)列表,以及一个来自我的产品数据库的现有(目标)产品列表,我想做三件事:

  1. Find all SKUs in the feed that are not in the target 查找供稿中不在目标中的所有SKU
  2. Find all SKUs that are in both, that are active feed products and update the target 查找这两个都是活动Feed产品的SKU,并更新目标
  3. Find all SKUs that are in both, that are inactive and soft delete from the target 查找同时处于非活动状态和从目标中进行软删除的所有SKU

What are the best practices for doing this without running a double loop? 在不运行双循环的情况下执行此操作的最佳实践是什么? Would prefer a LINQ 4 Objects solution as I already have my objects. 由于我已经有了对象,因此希望使用LINQ 4 Objects解决方案。

EDIT: BTW, I will need to transfer info from feed rows to target rows in the first 2 instances, just set a flag in the last instance. 编辑:顺便说一句,我将需要在前2个实例中将信息从提要行传输到目标行,只需在最后一个实例中设置一个标志即可。

TIA TIA

The LINQ-to-objects approach would be something like demonstration. LINQ到对象的方法将类似于演示。 Here I have two lists of strings and I want to pull matches, elements from alphas not in betas, and elements from betas not in alphas. 在这里,我有两个字符串列表,我想提取匹配项,alpha中的元素不在beta中,而beta中的元素不在alpha中。 The LINQ syntax is fairly simple. LINQ语法非常简单。

List<string> alphas = new List<string>() { "a", "b", "c", "d", "e" };
List<string> betas = new List<string>() { "a", "c", "e", "g", "i" };

var matches = from alpha in alphas
                join beta in betas
                on alpha equals beta
                select alpha;

var noBetas = from alpha in alphas
                join beta in betas
                on alpha equals beta
                into gj
                from b in gj.DefaultIfEmpty()
                where b == null
                select alpha;

var noAlphas = from beta in betas
                join alpha in alphas
                on beta equals alpha
                into gj
                from a in gj.DefaultIfEmpty()
                where a == null
                select beta;

The results of each is an IEnumerable<string> , and iterating over matches would reveal a, c, and e. 每个的结果都是IEnumerable<string> ,并且对匹配进行迭代将显示a,c和e。 noBetas would yield b and d. noBetas将产生b和d。 noAlphas would yield g and i. noAlphas将产生g和i。

I believe that's what you were asking for. 我相信这就是您要的。 Apply that to your lists of objects by joining on key fields instead of my simplistic scenario of one string equaling another. 通过连接关键字段,将其应用于您的对象列表,而不是将一个字符串等于另一个的简单化方案。

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

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