Create a List from Two Object Lists with Linq

Merge two List object into one List in Linq

Use Concat and OrderBy

var result = list1.Concat(list2).OrderBy(x => x.Elevation).ToList();

If you want to remove duplicates and get an unique set of elements you can also use Union method:

var result = list1.Union(list2).OrderBy(x => x.Elevation).ToList();

In order to make it work properly you need to overide Equals and GetHashCode methods in your class.

C# taking two lists of different objects to construct new list of objects

I think that you should use Join:

var result = dbBadgeList.Join(
dbBadgeProgress,
badge => badge.badgeId,
progress => progress.badgeId,
(badge, progress) => new dabUserBadgeProgress(badge, progress))
.ToList();

C# Linq query to join two lists and assign the result to new list

Based on my understanding of your question, I would like to propose two different approaches to solve it:

  • filtering the items in adGroups before associating the adGroups items with the gwtItems items
  • (filtering the items in gwtItems as you have already done, and then) associating the gwtItems items with the adGroups items

Depending on which variable (adGroups or gwtItems) benefits most from being filtered initially, you may choose which approach fits your use case best.

Filtering the adGroups items, then associating them with the gwtItems items

Here, all applicable items from adGroups are grouped by Sid before each Sid-based grouping is associated with items from gwtItems based on the gwtItem's Sid value. After associating adGroups groupings and gwtItems with equal Sid (and unequal Name, due to the initial filtering of adGroups) using .Join(), we select each associated adGroup and gwtItem pair's adGroup.Name and gwtItem.Id in a tuple and use SelectMany() to flatten the collection (from a IEnumerable<IEnumerable< * >> to a IEnumerable< * >).

public IEnumerable<( string Name, int Id )> GetWorkTeamsWhereChangeName(
IEnumerable<Group> adGroups,
IEnumerable<GuidelinesWorkTeam> gwtItems)
{
return adGroups
.Where(adGroup => gwtItems.Any(gwtItem =>
gwtItem.Sid == adGroup.Sid &&
gwtItem.Name != adGroup.Name))
.GroupBy(adGroup => adGroup.Sid)
.Join(gwtItems,
grouping => grouping.Key,
gwtItem => gwtItem.Sid,
( grouping, gwtItem ) => grouping
.Select(adGroup => ( adGroup.Name, gwtItem.Id )))
.SelectMany(_ => _);
}

Associating the gwtItems items with the adGroups items (after filtering the gwtItems first)

Here, the items from gwtItems are first associated with the items from adGroups based on the Sid property from the respective classes, using .Join() before a tuple containing relevant information is returned from the .Join() operation; then, only the associated items that actually have different names are selected and converted to a simpler tuple.

Your original filtering should possibly be included if it is probable that it will filter out many items from gwtItems; but it shouldn't be necessary to obtain the desired result.

public IEnumerable<( string Name, int Id )> GetWorkTeamsWhereChangeName(
IEnumerable<Group> adGroups,
IEnumerable<GuidelinesWorkTeam> gwtItems)
{
return gwtItems
// Original filtering
.Where(gwtItem => adGroups.Any(adGroup =>
gwtItem.Sid == adGroup.Sid &&
gwtItem.Name != adGroup.Name))
// Association and final filtering
.Join(adGroups,
gwtItem => gwtItem.Sid,
adGroup => adGroup.Sid,
( gwtItem, adGroup ) => (
ChangedName: gwtItem.Name != adGroup.Name,
Name: adGroup.Name,
Id: gwtItem.Id))
.Where(workTeam => workTeam.ChangedName)
.Select(workTeam => ( workTeam.Name, workTeam.Id ));
}

c# linq create a third list combining two lists with different item count


var List3 = (from f in List1
from s in List2
select $"{f}, {s}").ToList();

Join two lists of objects: All properties from one list, only certain properties from the other one

I would suggest to make a third class which represents the joined parameter set.

public class Class1_2
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int Amount { get; set; }
public string Category { get; set; }

public DateTime UpdateTime { get; set; }
public int ItemState { get; set; }
}

How to join 2 Lists is described in LINQ Join 2 Lists. So you would end up with something like this:

List<Class1_2> joinedList = (from item1 in listClass1
join item2 in listClass2
on item1.ProductId equals item2.ProductId
select new Class1_2
{
ProductId = item1.ProductId,
Amount = item1.Amount,
ProductName = item1.ProductName,
Category = item1.Category,
ItemState = item2.ItemState,
UpdateTime = item2.UpdateTime
}).ToList();

Explanation: You join the items on the particular property. If it is equal in both lists you take the item and construct a new one with the selected properties. The join would also work without Class1_2 but then you get a result of dynamic type. The advantage of the new Class_1_2 is that you can use it for serialization with a defined structure.

EDIT

you could also use a second constructor in Class1_2

public Class1_2(Class1 c1, Class2 c2)
{
ProductId = c1.ProductId;
Amount = c1.Amount;
ProductName = c1.ProductName;
Category = c1.Category;
ItemState = c2.ItemState;
UpdateTime = c2.UpdateTime;
}

that would simplify a little the select statement to:

select new Class1_2(item1, item2)).ToList();

How to use linq to assign two lists and an int to create one list of a new object?

You can use LINQ Zip (refer)

Sample below:

var result = keys.Zip(reportsKey, 
(key, reportKey) => new MyObject
{
Key = key,
ReportKey = reportKey,
ReportType = reportType
})
.ToList();


Related Topics



Leave a reply



Submit