Linq Join 2 List<T>S

how do I join two lists using linq or lambda expressions

It sounds like you want something like:

var query = from order in workOrders
join plan in plans
on order.WorkOrderNumber equals plan.WorkOrderNumber
select new
{
order.WorkOrderNumber,
order.Description,
plan.ScheduledDate
};

LINQ Join 2 ListTs

Joins are not so difficult, but your problem could probably use some further explanation.

To join two lists, you could do something like

var joined = from Item1 in list1
join Item2 in list2
on Item1.Id equals Item2.Id // join on some property
select new { Item1, Item2 };

this will give an IEnumerable<'a>, where 'a is an anonymous type holding an item from list1 and its related item from list2. You could then choose which objects' properties to use as needed.

To get the result to a concrete list, all that is needed is a call to .ToList(). You can do that like

var list3 = joined.ToList();
// or
var list3 = (from Item1 in list1
join Item2 in list2
on Item1.Id equals Item2.Id // join on some property
select new { Item1, Item2 }).ToList();

To do a left join to select all elements from list1 even without a match in list2, you can do something like this

var list3 = (from Item1 in list1
join Item2 in list2
on Item1.Id equals Item2.Id // join on some property
into grouping
from Item2 in grouping.DefaultIfEmpty()
select new { Item1, Item2 }).ToList();

This will give you a list where Item1 equals the item from the first list and Item2 will either equal the matching item from the second list or the default, which will be null for a reference type.

Joining two lists together

You could try:

List<string> a = new List<string>();
List<string> b = new List<string>();

a.AddRange(b);

MSDN page for AddRange

This preserves the order of the lists, but it doesn't remove any duplicates (which Union would do).

This does change list a. If you wanted to preserve the original lists then you should use Concat (as pointed out in the other answers):

var newList = a.Concat(b);

This returns an IEnumerable as long as a is not null.

join 2 lists and create one to many relationship

Not sure if this is what you are looking for, but you can use Select and SelectMany to create the final List of KeyVaulePair.

        var list1 = new List<string>() { "High", "Medium", "Low" };
var list2 = new List<string>() { "Submitted", "In-Progress", "Draft", "Rejected" };

var joined = list1.SelectMany(val1 => list2.Select(val2 => new KeyValuePair<string, string>(val1, val2)));
foreach(var res in joined)
{
Console.WriteLine(res.Key + "," + res.Value);
}

If you want to use imperative style of coding, then just create 2 for loops

Join two lists using linq queries - C#

It looks like there is no query equivalent for Union method. You will need to use this method either in method chain call or in your query.

If you look at MSDN documentation on returning the set union of two sequences, you will see the following official query:

var infoQuery =
(from cust in db.Customers
select cust.Country)
.Union
(from emp in db.Employees
select emp.Country)
;

So, there are only two options in your case:

  1. Method chain:

    var joined = customers1.Union(customers2);
  2. LINQ query

    var joined = (from c1 in customers1
    select c1)
    .Union
    (from c2 in customers2
    select c2);

Can I merge two lists using Linq?

Here is a method that produces the correct output. Is there an easier way to do this? Can this be done with Linq only?

private void DoTest()
{
var packItems = new List<PackItem>()
{
new PackItem() {ID = 4, Quantity = 5, Length = "10"},
new PackItem() {ID = 5, Quantity = 2, Length = "4"},
new PackItem() {ID = 6, Quantity = 1, Length = "4"}
};
var productionInfoList = new List<ProductionInfo>()
{
new ProductionInfo() { CoilNum = "A", Quantity = 4, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 1, Length = "10"},
new ProductionInfo() { CoilNum = "B", Quantity = 2, Length = "4"},
new ProductionInfo() { CoilNum = "A", Quantity = 1, Length = "4"},
};

//first create a list with one item for each pieces
var individualProduction = new List<ProductionInfo>();
foreach (var item in productionInfoList)
{
for (int i = 0; i < item.Quantity; i++)
{
individualProduction.Add(new ProductionInfo()
{
Quantity = 1,
Length = item.Length,
CoilNum = item.CoilNum
});
}
}
//next loop through and assign all the pack line ids
foreach (var item in individualProduction)
{
var packItem = packItems.FirstOrDefault(i => i.Quantity > 0 && i.Length == item.Length);
if (packItem != null)
{
packItem.Quantity -= 1;
item.LineID = packItem.ID;
}
else
{
item.Quantity = 0;
}
}
//now group them back into a merged list
var grouped = individualProduction.GroupBy(i => (i.CoilNum, i.LineID, i.Length));
//output the merged list
var merged1 = grouped.Select(g => new ProductionInfo()
{
LineID = g.Key.LineID,
CoilNum = g.Key.CoilNum,
Length = g.Key.Length,
Quantity = g.Count()
});
}

Join two lists and select nested items with linq

Try this:

IEnumerable<string> queries = LandingSilo.Relationships
.Where(r => r.RelationshipType == 1)
.Join(
LandingSilo.Nodes,
r => r.SlaveKey,
n => n.Key,
(r, n) => n.Queries.SingleOrDefault(q => q.Seq == r.QueryId))
.Where(q => q != null)
.Select(q => q.Query);

Line by line: filter all Relationships with type different from 1, join on SlaveKey/Key and select the only query in the node that has the Seq equal to the Relationships QueryId. Filter out null results and select the Query property. This is going to throw an InvalidOperationException if there are multiple queries within one node matching.

This can be also done in the LINQ keyword syntax like this:

IEnumerable<string> queries = 
from r in LandingSilo.Relationships
where r.RelationshipType == 1
join n in LandingSilo.Nodes on r.SlaveKey equals n.Key
from q in n.Queries.SingleOrDefault(q => q.Seq == r.QueryId)
where q != null
select q.Query;


Related Topics



Leave a reply



Submit