Anoop Joshi P
Anoop Joshi P

Reputation: 25527

linq query for set attribute

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

Upvotes: 1

Views: 354

Answers (8)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

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];

Upvotes: 1

Dustin Kingen
Dustin Kingen

Reputation: 21245

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.

Upvotes: 1

user2704193
user2704193

Reputation:

Try this:

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

Upvotes: 1

alergy
alergy

Reputation: 974

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;

Upvotes: 4

Hilmi
Hilmi

Reputation: 3441

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

Upvotes: 2

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

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;
    }
}

Upvotes: 1

proggrock
proggrock

Reputation: 3289

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

Upvotes: 1

Anand
Anand

Reputation: 14915

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.

Upvotes: 2

Related Questions