나는 이것에 대한 대답을 둘러 보았고 내가 찾은 것은 내가하고있는 것과 비슷하지만 제대로 이해하지 못했습니다. 두 개체가 목록 일 때 결합 할 수 있지만 단일 개체 일 때는 결합 할 수 없습니다. 이게 말이되는지 잘 모르겠어요
여기에 두 개의 엔티티 객체가 있습니다.
Location location = VIPEF.Locations
.Where(w => w.LocationID == id).FirstOrDefault<Location>();
Contact contact = VIPEF.Contacts
.Where(w => w.ContactID == location.ContactID).FirstOrDefault<Contact>();
하지만 쿼리를 작성하면 :
var query = from a in location
join b in contact on a <-- Thats where I get a problem
나는이 두 가지를 결합로 이동하면 나는 위치에서 ID를 얻을 수 있고, 내가 얻을 수있는 유일한 옵션은 a.equals
아니라 a.LocationID
내가 점점해야한다고 생각한다.
내가 여기서 뭘 잘못하고 있니?
두 개체를 결합하는 데 Linq가 필요하지 않고 새 개체를 구성하기 만하면됩니다. 객체 이니셜 라이저 구문을 사용하는 것은 매우 간단 합니다 .
var thing = new Thing //<-- Your class that has the combined properties you need
{
LocationID = location.LocationID,
LocationName = location.Name,
ContactName = contact.Name,
ContactAddress = contact.Address,
//etc...
};
또는 두 개의 쿼리가 아닌 한 번에이 모든 작업을 수행 한 다음 구성 할 수 있습니다.
var things = from l in VIPEF.Locations
join c in VIPEF.Contacts on l.ContactID equals c.ContactID
where l.LocationID == id
select new Thing
{
LocationID = location.LocationID,
LocationName = location.Name,
ContactName = contact.Name,
ContactAddress = contact.Address,
//etc...
};
이것은 당신이 줄 것이다 IEnumerable<Thing>
그것은 단지 하나의 항목이있을 수 있지만 함께 플레이 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다