I have one class to bind for ex.
public class MyClass
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Now, this class will get values from different tables but they have common ID.
here is the Lists that I am getting in my loop.
var List1 = item.TicketTypes.Where(m => m.PerformanceID == item.Id)
.Select(t => new { t.Name, t.ID});
var List2 = item.Pricies.Where(m => m.PerformanceID == item.Id)
.Select(t => new { t.Price, t.ID });
item is object of for each loop instance.
What I want is,
I want List of MyClass filled with Entity Name and Price as per their ID.
There isn't much informations here for me to test this, but I have this to suggest:
// Make sure to add a constructor to MyClass
var List1 = item.TicketTypes.Where(m => m.PerformanceID == item.Id)
.Select(t => new MyClass(t.Name, t.ID, item.Pricies.FirstOrDefault(p => p.PerformanceID == t.ID).Price));
This should give you the desired result, but like I said I don't have enough informations to test this so make sure you comment the problems so we can fix them together.
In a Linq Statement, if my assumptions are right(cannot test it):
var result= from t1 in item.TicketTypes
join t2 in item.Pricies on t1.ID equals t2.ID
select new MyClass() { Id = t1.ID, Name = t1.Name, Price = t2.Price };
use .ToList()
if you need a List
Try this :
var query = List1.Join(List2, l1 => l1.Id, l2 => l2.Id, (l1, l2) => new { ID = l1.Id, Name = l1.Name, Price = l2.Price });
foreach (var obj in query)
{
Console.WriteLine("{0} - {1} - {2}", obj.ID, obj.Name, obj.Price);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.