Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Objects Join two collections to set values in the first collection

Tags:

I have the following Entity Framework query:

var results = from r in db.Results               select r; 

I'm using AutoMapper to map to another type:

var mapped = Mapper.Map<IEnumerable<Database.Result>, IEnumerable<Objects.Result>>(results); 

In my Objects.Result type, I have a property called reason that is not coming from the database. It is coming from another source that I need to basically populate back into my mapped type:

var reasons = new List<Reason> {     new Reason { Id = 1, Reason = "asdf..." } }; 

I need to join the reasons with my mapped collection and set the Reason property in my mapped collection using the value from my reasons collection. Is this possible?

 // need something like this:  mapped = from m in mapped           join r in reasons on m.Id equals r.Id           update m.Reason = r.Reason           select m; 

Obviously the above code doesn't compile, but is there code I can write that does what I want?

like image 941
Dismissile Avatar asked Nov 14 '11 20:11

Dismissile


People also ask

Which function is used to get first record in a LINQ collection?

LINQ Syntax - Fluent vs. The . First() method does just what you think it does – it gets the first element in the list.


2 Answers

Do the mutation in a loop. Optimally, Linq should be free of mutations to the collection(s) it operates against. Use Linq to filter, order, project your data, use traditional techniques to modify.

var joinedData = from m in mapped                   join r in reasons on m.Id equals r.Id                   select new { m, r };  foreach (var item in joinedData) {     item.m.Reason = item.r.Reason; } 
like image 110
Anthony Pegram Avatar answered Sep 19 '22 10:09

Anthony Pegram


This may save lot of your time. Below code is for Join two collections and to set property value of first collection.

class SourceType {     public int Id;     public string Name;     public int Age { get; set; }     // other properties }  class DestinationType {     public int Id;     public string Name;     public int Age { get; set; }     // other properties }     List<SourceType> sourceList = new List<SourceType>();     sourceList.Add(new SourceType { Id = 1, Name = "1111", Age = 35});     sourceList.Add(new SourceType { Id = 2, Name = "2222", Age = 26});     sourceList.Add(new SourceType { Id = 3, Name = "3333", Age = 43});     sourceList.Add(new SourceType { Id = 5, Name = "5555", Age = 37});      List<DestinationType> destinationList = new List<DestinationType>();     destinationList.Add(new DestinationType { Id = 1, Name = null });     destinationList.Add(new DestinationType { Id = 2, Name = null });     destinationList.Add(new DestinationType { Id = 3, Name = null });     destinationList.Add(new DestinationType { Id = 4, Name = null });       var mapped= destinationList.Join(sourceList, d => d.Id, s => s.Id, (d, s) =>     {         d.Name = s.Name;         d.Age = s.Age;         return d;     }).ToList(); 
like image 20
Towhidul Islam Tuhin Avatar answered Sep 17 '22 10:09

Towhidul Islam Tuhin