I have three classes like this :
public class Cancellations
{
public int ID {get; set;}
public string CustomerID {get; set;}
public string CustomerName {get; set;}
}
public class Service
{
public int ID {get; set;}
public string CustomerID2 {get; set;}
public string ServiceName {get; set;}
}
public class Bill
{
public int ID {get; set;}
public string CustomerID3 {get; set;}
public string City {get; set;}
}
There is such a relation in these databases : Service.ID=Cancellations.CustomerID
and Bill.CustomerID3 = Service.CustomerID2
. What I'm trying to achieve is that how can I get the correct city when I have CustomerID in Cancellations object. Can you give me tips about that? Thanks.
Suppose you have the CustomerID then:
With C# and Linq
using System.Linq;
--
// Suppose this is your cancellation object's CustomerID
int _customerID = 123;
--
var resultObj = from billObj in Bills
join serviceObj in Service on Service.CustomerID2 equals billObj.CustomerID3
join cancellationObj in Cancellation on Cancellation.CustomerID equals serviceObj.ID
where CancellationObj.CustomerID == _customerID
select new { Bills = billObj };
--
String City = resultObj.Select(x => x.City).FirstOrDefault();
I would suggest you give your objects more meaningful names if possible.
Just a straight forward join:
var result = (from c in db.Cancellations
join s in db.Service on c.CustomerID equals s.ID
join b in db.Bill on s.CustomerID2 equals b.CustomerID3
select new { c.Id, c.CustomerName, b.City }).ToList();
2 recommendations:
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.