如何在不选择新实例的情况下将一个查询的结果加入另一个查询的结果? [英] How to join the result of one query into the result of another without selecting new instance?

查看:38
本文介绍了如何在不选择新实例的情况下将一个查询的结果加入另一个查询的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中的数据来自两个不同的来源-一个是数据库,另一个是Web API.
数据库源为我提供了大部分数据,而Web API仅提供了一些属性.
我使用Dapper以IEnumerable<MyClass>的形式从数据库中获取数据(其中,Web API的属性均为空), 并且来自Web API的数据是IEnumerable<WebApiClass>.

I have a class where it's data comes from two difference sources - one is a database and the other is a web API.
The database source gives me most of the data, and the web API just a few properties.
I get the data from the database using Dapper, as an IEnumerable<MyClass> (where the properties from the web API are all nulls), and the data from the web API is an IEnumerable<WebApiClass>.

现在我需要将这两个结果合并为一个IEnuemrable<MyClass>-足够简单-

Now I need to join these two results to a single IEnuemrable<MyClass> - simple enough -

var query = from c in dbResults
            join w in webResults on c.Id equals w.Id
            select new MyClass()
            {
                dbProp1 = c.dbProp1, dbProp2 = c.dbProp2, ...
                waProp1 = w.Prop1, waProp2 = w.Prop2, ...
            }

是否有一种方法可以不选择new MyClass()而只是使用dbResults中已经存在的MyClass实例?

Is there a way to do that without selecting a new MyClass(), but simply use the already existing instances of MyClass from dbResults?

我见过的所有联接查询都使用select new-这真的是唯一的选择吗?

All the join queries I've seen use select new - is that really the only option?

推荐答案

使用方法语法联接并显式打开resultSelector的范围.在其中编辑所需的对象,然后将其返回.

Use method syntax join and explicitly open the scope of the resultSelector. In it edit the object as you want and return it.

var result = dbResults.Join(webResults, db => db.Id, web => web.Id
                            (db, web) => {
                                db.SomePropFromWeb = web.SomeProp;
                                return db;
                            });

这篇关于如何在不选择新实例的情况下将一个查询的结果加入另一个查询的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆