1

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>.

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, ...
            }

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

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

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • I think your question can be worded a little differently, because what you really want is to `select c` with updated properties based on `w`-- Which has [already been asked and answered](https://stackoverflow.com/q/807797/2679750) – Mark C. Jan 29 '18 at 21:08
  • @MarkC. Thanks, I did try rephrasing my search, but that one just didn't came up. – Zohar Peled Jan 30 '18 at 08:17
  • No problem at all – Mark C. Jan 30 '18 at 13:52

1 Answers1

1

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;
                            });
Gilad Green
  • 36,708
  • 7
  • 61
  • 95