1

I have 2 class lists,

List<CommunityGroups> List1=new List<CommunityGroups>(); List<CommunityGroups> List1=new List<CommunityGroups>(); public class CommunityGroups { public long CommunityID { get; set; } public string CommunityName { get; set; } } 

In these lists, List1 10 CommunityGroups which has both CommunityID and CommunityName. List2 contais 5 CommunityGroups which has CommunityID and blank CommunityNames. I need to populate the CommunityNames in List2 with respect to List1. Now I am using the code,

for (int i = 0; i < List2.Count; i++) { for (int j = 0; j < List1.Length; j++) { if (List2[i].GroupId == List1[j].Id) { List2[i].GroupName = List1[j].Name; } } } } 

I need to use linq query for this purpose. How can I replace these code with linq. Please someone help me.

Thanks

2
  • Where are the GroupId, GroupName, Name and Id defined in CommunityGroups? Commented Sep 23, 2013 at 12:59
  • Why do you have to use Linq? Linq is for querying, not for setting properties. Commented Sep 23, 2013 at 13:00

8 Answers 8

4

You could query the two lists with a join, and then run through the enumerated pairs making the assignment:

var combineditems = from item1 in List1 join item2 in List2 on item1.Id equals item2.GroupId select new { item1 , item2 }; foreach(var items in combineditems) items.item2.GroupName = items.item2.Name; 
Sign up to request clarification or add additional context in comments.

Comments

2

You can just filter the rows from first list as it contains both Id and Name where id is in second list and create a new list or assign it to List2.

List2 = List1.Where( x => List2.Contains(x.Id)).ToList(); 

You can also do a join from list1 and list2 and then select name and description. Compare in terms of performance and choose any method that you like.

Comments

2
var newList = List2.Foreach( x => x.Name = List1.First(m => m.Id == x.Id).Name); 

Comments

1

Translation (changed names so they match class definition) with maximized linq:

foreach (CommunityGroups t in List2) { foreach (CommunityGroups t1 in List1.Where(t1 => t.GroupId == t1.GroupId)) { t.GroupName = t1.GroupName; } } 

Comments

1
 foreach (CommunityGroups t1 in List2) { foreach (var t in List1.Where(t => t1.GroupId == t.Id)) { t1.GroupName = t.Name; } } 

Comments

1

Try this:

var query = from l1 in List1 join l2 in List2 on l1.CommunityID equals l2.CommunityID select l2; 

1 Comment

This is a only query definition, which will not be executed. Also this query, even if you will execute it, do not update list2 items.
1

In general LINQ statements do not contain side effects.

However, it is possible to write a statement such as:

list2.Join(list1, l2 => l2.CommunityID, l1 => l1.CommunityID, (item2, item1) => { item2.CommunityName = item1.CommunityName; return item2; } ).ToList(); 

I would recommend the foreach approach since it conveys the correct meaning of mutability.

Comments

1

You can convert first list to dictionary with id as key and name as value:

var names = List1.ToDictionary(l1 => l1.CommunityID, l1 => l1.CommunityName); foreach (var l2 in List2) if (names.ContainsKey(l2.CommunityID)) l2.CommunityName = names[l2.CommunityID]; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.