converting a sql server view to linq












0















I'm not sure if this is the right forum, if not please move it
My office is in the process of re-writing our app and switching from VB to C# and the entity framework. I've had some success in joining multiple tables all with left outer joins but I'm at my limits on how to convert an sql view like the following:



SELECT        dbo.Addresses.AddressId, dbo.AddressTypes.AddressType, dbo.Addresses.AddressTypeId, dbo.Addresses.ParentAddressId, dbo.Addresses.AddressCode, dbo.Addresses.AddressNumber, dbo.Addresses.Address, 
dbo.Addresses.SubAddress, dbo.Addresses.Direction, dbo.Addresses.City, dbo.Addresses.StateId, dbo.Addresses.CountryId, ISNULL(dbo.Addresses.AddressNumber + ' ', '') + ISNULL(dbo.Addresses.Direction + ' ', '')
+ ISNULL(dbo.Addresses.Address + ' ', '') + ISNULL(dbo.Addresses.Suffix + ' ', '') + ISNULL(dbo.Addresses.SubAddress + ' ', '') AS FullAddress, dbo.Addresses.RegionId, dbo.Addresses.CountyId,
dbo.Addresses.OccupancyTypeId, dbo.Addresses.PropertyUseTypeId, dbo.Addresses.Comment, dbo.States.StateAbbr, dbo.States.State, dbo.Regions.Region, dbo.Regions.RegionCode, dbo.Counties.County,
dbo.Counties.CountyCode, dbo.Countries.Country, dbo.OccupancyTypes.OccupancyType, dbo.OccupancyTypes.OccupancyTypeCode, dbo.PropertyUseTypes.PropertyUseType, dbo.Party.PartyName,
dbo.PropertyUseTypes.PropertyUseTypeCode, dbo.UserDefFields.UserDefFieldId, dbo.UserDefFields.FieldDesc, dbo.UserDefValues.UserDefValueId, dbo.UserDefValues.UserDefValue, dbo.Addresses.ZipId, dbo.Zips.Zip,
dbo.AddressParties.PartyID, dbo.Addresses.Latitude, dbo.Addresses.Longitude, dbo.Addresses.Inactive, dbo.Addresses.DefaultPass, dbo.Addresses.Suffix, dbo.AddressTypes.AgencyId, dbo.Addresses.LegalDesc,
dbo.AddressParties.Inactive AS PAInactive, dbo.AddressParties.RoleTypeId, dbo.Addresses.POBox, dbo.AddressParties.ExternalValue, dbo.Addresses.DateUpdated, dbo.Addresses.DateInserted, dbo.Addresses.ReportId,
dbo.Addresses.Map, dbo.Addresses.Block, dbo.Addresses.Lot, dbo.Addresses.TaxParcel, dbo.Addresses.ExternalId, dbo.Addresses.Schedule
FROM dbo.UserDefFields RIGHT OUTER JOIN
dbo.UserDefValues ON dbo.UserDefFields.UserDefFieldId = dbo.UserDefValues.UserDefFieldId RIGHT OUTER JOIN
dbo.Party RIGHT OUTER JOIN
dbo.Addresses LEFT OUTER JOIN
dbo.Zips ON dbo.Addresses.ZipId = dbo.Zips.ZipId LEFT OUTER JOIN
dbo.AddressTypes ON dbo.Addresses.AddressTypeId = dbo.AddressTypes.AddressTypeId LEFT OUTER JOIN
dbo.States ON dbo.Addresses.StateId = dbo.States.StateId LEFT OUTER JOIN
dbo.Regions ON dbo.Addresses.RegionId = dbo.Regions.RegionId LEFT OUTER JOIN
dbo.Counties ON dbo.Addresses.CountyId = dbo.Counties.CountyId LEFT OUTER JOIN
dbo.Countries ON dbo.Addresses.CountryId = dbo.Countries.CountryId LEFT OUTER JOIN
dbo.OccupancyTypes ON dbo.Addresses.OccupancyTypeId = dbo.OccupancyTypes.OccupancyTypeId LEFT OUTER JOIN
dbo.PropertyUseTypes ON dbo.Addresses.PropertyUseTypeId = dbo.PropertyUseTypes.PropertyUseTypeId LEFT OUTER JOIN
dbo.AddressParties ON dbo.Addresses.AddressId = dbo.AddressParties.AddressID ON dbo.Party.PartyID = dbo.AddressParties.PartyID ON dbo.UserDefValues.RecordId = dbo.Addresses.AddressId


Into something like the following:



   public List<AddressPartyList> GetAddressPartyList(Guid? AddressId, Guid? RoleTypeId = null, bool ShowInactive = false, bool FromWebOnly = false, bool WebAcceptedOnly = false, bool WebRejectedOnly = false)
{
IQueryable<AddressPartyList> thisList;

thisList = myappContext.myappData().AddressParties.Join(
myappContext.myappData().RoleTypes, ap => ap.RoleTypeId, rt => rt.RoleTypeId, (ap, rt) => new { ap, rt }).Join(
myappContext.myappData().Parties, apr => apr.ap.PartyId, p => p.PartyId, (apr, p) => new AddressPartyList
{
AddressId = apr.ap.AddressId,
PartyId = apr.ap.PartyId,
AccountId = p.AccountId,
RoleType = apr.rt.RoleType1,
Salutation = p.Salutation,
FirstName = p.FirstName,
MiddleInitial = p.MiddleInitial,
LastName = p.LastName,
Suffix = p.Suffix,
PartyName = p.PartyName,
Email = p.Email,
Comment = p.Comment,
ExternalId = p.ExternalId,
PartyInactive = p.Inactive,
WebAccountId = p.WebAccountId,
DateUpdated = p.DateUpdated,
DateInserted = p.DateInserted,
PriceLevel = p.PriceLevel,
FromWeb = p.FromWeb,
WebAccepted = p.WebAccepted,
WebRejected = p.WebRejected,
RoleTypeId = apr.ap.RoleTypeId,
InactiveAtAddress = apr.ap.Inactive,
IsBus = p.IsBus,
Sequence = apr.ap.Sequence,
AddressPartyId = apr.ap.AddressPartyId

}).Where(p => p.AddressId == AddressId);


if (!ShowInactive)
{
thisList = thisList.Where(p => p.PartyInactive == false && p.InactiveAtAddress == false);
}

if (RoleTypeId != null)
{
thisList = thisList.Where(p => p.RoleTypeId == RoleTypeId);
}

if (FromWebOnly)
{
thisList = thisList.Where(p => p.FromWeb == true);
}
if (WebAcceptedOnly)
{
thisList = thisList.Where(p => p.WebAccepted == true);
}
if (WebRejectedOnly)
{
thisList = thisList.Where(p => p.WebRejected == true);
}

return thisList.GroupBy(p => p.PartyId).Select(p => p.FirstOrDefault()).OrderBy(p => p.Sequence).ThenBy(p => p.PartyName).ToList();
}


Any help would be appreciated. Also, what would be the syntax for a view with multiple right and left outer joins. I didn't code it, I'm just trying to convert it and I understand it's best to use left outer joins.
Thanks.










share|improve this question

























  • First and foremost: don't join, use navigation properties.

    – Gert Arnold
    Dec 28 '18 at 14:56











  • You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

    – NetMage
    Dec 28 '18 at 19:23
















0















I'm not sure if this is the right forum, if not please move it
My office is in the process of re-writing our app and switching from VB to C# and the entity framework. I've had some success in joining multiple tables all with left outer joins but I'm at my limits on how to convert an sql view like the following:



SELECT        dbo.Addresses.AddressId, dbo.AddressTypes.AddressType, dbo.Addresses.AddressTypeId, dbo.Addresses.ParentAddressId, dbo.Addresses.AddressCode, dbo.Addresses.AddressNumber, dbo.Addresses.Address, 
dbo.Addresses.SubAddress, dbo.Addresses.Direction, dbo.Addresses.City, dbo.Addresses.StateId, dbo.Addresses.CountryId, ISNULL(dbo.Addresses.AddressNumber + ' ', '') + ISNULL(dbo.Addresses.Direction + ' ', '')
+ ISNULL(dbo.Addresses.Address + ' ', '') + ISNULL(dbo.Addresses.Suffix + ' ', '') + ISNULL(dbo.Addresses.SubAddress + ' ', '') AS FullAddress, dbo.Addresses.RegionId, dbo.Addresses.CountyId,
dbo.Addresses.OccupancyTypeId, dbo.Addresses.PropertyUseTypeId, dbo.Addresses.Comment, dbo.States.StateAbbr, dbo.States.State, dbo.Regions.Region, dbo.Regions.RegionCode, dbo.Counties.County,
dbo.Counties.CountyCode, dbo.Countries.Country, dbo.OccupancyTypes.OccupancyType, dbo.OccupancyTypes.OccupancyTypeCode, dbo.PropertyUseTypes.PropertyUseType, dbo.Party.PartyName,
dbo.PropertyUseTypes.PropertyUseTypeCode, dbo.UserDefFields.UserDefFieldId, dbo.UserDefFields.FieldDesc, dbo.UserDefValues.UserDefValueId, dbo.UserDefValues.UserDefValue, dbo.Addresses.ZipId, dbo.Zips.Zip,
dbo.AddressParties.PartyID, dbo.Addresses.Latitude, dbo.Addresses.Longitude, dbo.Addresses.Inactive, dbo.Addresses.DefaultPass, dbo.Addresses.Suffix, dbo.AddressTypes.AgencyId, dbo.Addresses.LegalDesc,
dbo.AddressParties.Inactive AS PAInactive, dbo.AddressParties.RoleTypeId, dbo.Addresses.POBox, dbo.AddressParties.ExternalValue, dbo.Addresses.DateUpdated, dbo.Addresses.DateInserted, dbo.Addresses.ReportId,
dbo.Addresses.Map, dbo.Addresses.Block, dbo.Addresses.Lot, dbo.Addresses.TaxParcel, dbo.Addresses.ExternalId, dbo.Addresses.Schedule
FROM dbo.UserDefFields RIGHT OUTER JOIN
dbo.UserDefValues ON dbo.UserDefFields.UserDefFieldId = dbo.UserDefValues.UserDefFieldId RIGHT OUTER JOIN
dbo.Party RIGHT OUTER JOIN
dbo.Addresses LEFT OUTER JOIN
dbo.Zips ON dbo.Addresses.ZipId = dbo.Zips.ZipId LEFT OUTER JOIN
dbo.AddressTypes ON dbo.Addresses.AddressTypeId = dbo.AddressTypes.AddressTypeId LEFT OUTER JOIN
dbo.States ON dbo.Addresses.StateId = dbo.States.StateId LEFT OUTER JOIN
dbo.Regions ON dbo.Addresses.RegionId = dbo.Regions.RegionId LEFT OUTER JOIN
dbo.Counties ON dbo.Addresses.CountyId = dbo.Counties.CountyId LEFT OUTER JOIN
dbo.Countries ON dbo.Addresses.CountryId = dbo.Countries.CountryId LEFT OUTER JOIN
dbo.OccupancyTypes ON dbo.Addresses.OccupancyTypeId = dbo.OccupancyTypes.OccupancyTypeId LEFT OUTER JOIN
dbo.PropertyUseTypes ON dbo.Addresses.PropertyUseTypeId = dbo.PropertyUseTypes.PropertyUseTypeId LEFT OUTER JOIN
dbo.AddressParties ON dbo.Addresses.AddressId = dbo.AddressParties.AddressID ON dbo.Party.PartyID = dbo.AddressParties.PartyID ON dbo.UserDefValues.RecordId = dbo.Addresses.AddressId


Into something like the following:



   public List<AddressPartyList> GetAddressPartyList(Guid? AddressId, Guid? RoleTypeId = null, bool ShowInactive = false, bool FromWebOnly = false, bool WebAcceptedOnly = false, bool WebRejectedOnly = false)
{
IQueryable<AddressPartyList> thisList;

thisList = myappContext.myappData().AddressParties.Join(
myappContext.myappData().RoleTypes, ap => ap.RoleTypeId, rt => rt.RoleTypeId, (ap, rt) => new { ap, rt }).Join(
myappContext.myappData().Parties, apr => apr.ap.PartyId, p => p.PartyId, (apr, p) => new AddressPartyList
{
AddressId = apr.ap.AddressId,
PartyId = apr.ap.PartyId,
AccountId = p.AccountId,
RoleType = apr.rt.RoleType1,
Salutation = p.Salutation,
FirstName = p.FirstName,
MiddleInitial = p.MiddleInitial,
LastName = p.LastName,
Suffix = p.Suffix,
PartyName = p.PartyName,
Email = p.Email,
Comment = p.Comment,
ExternalId = p.ExternalId,
PartyInactive = p.Inactive,
WebAccountId = p.WebAccountId,
DateUpdated = p.DateUpdated,
DateInserted = p.DateInserted,
PriceLevel = p.PriceLevel,
FromWeb = p.FromWeb,
WebAccepted = p.WebAccepted,
WebRejected = p.WebRejected,
RoleTypeId = apr.ap.RoleTypeId,
InactiveAtAddress = apr.ap.Inactive,
IsBus = p.IsBus,
Sequence = apr.ap.Sequence,
AddressPartyId = apr.ap.AddressPartyId

}).Where(p => p.AddressId == AddressId);


if (!ShowInactive)
{
thisList = thisList.Where(p => p.PartyInactive == false && p.InactiveAtAddress == false);
}

if (RoleTypeId != null)
{
thisList = thisList.Where(p => p.RoleTypeId == RoleTypeId);
}

if (FromWebOnly)
{
thisList = thisList.Where(p => p.FromWeb == true);
}
if (WebAcceptedOnly)
{
thisList = thisList.Where(p => p.WebAccepted == true);
}
if (WebRejectedOnly)
{
thisList = thisList.Where(p => p.WebRejected == true);
}

return thisList.GroupBy(p => p.PartyId).Select(p => p.FirstOrDefault()).OrderBy(p => p.Sequence).ThenBy(p => p.PartyName).ToList();
}


Any help would be appreciated. Also, what would be the syntax for a view with multiple right and left outer joins. I didn't code it, I'm just trying to convert it and I understand it's best to use left outer joins.
Thanks.










share|improve this question

























  • First and foremost: don't join, use navigation properties.

    – Gert Arnold
    Dec 28 '18 at 14:56











  • You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

    – NetMage
    Dec 28 '18 at 19:23














0












0








0








I'm not sure if this is the right forum, if not please move it
My office is in the process of re-writing our app and switching from VB to C# and the entity framework. I've had some success in joining multiple tables all with left outer joins but I'm at my limits on how to convert an sql view like the following:



SELECT        dbo.Addresses.AddressId, dbo.AddressTypes.AddressType, dbo.Addresses.AddressTypeId, dbo.Addresses.ParentAddressId, dbo.Addresses.AddressCode, dbo.Addresses.AddressNumber, dbo.Addresses.Address, 
dbo.Addresses.SubAddress, dbo.Addresses.Direction, dbo.Addresses.City, dbo.Addresses.StateId, dbo.Addresses.CountryId, ISNULL(dbo.Addresses.AddressNumber + ' ', '') + ISNULL(dbo.Addresses.Direction + ' ', '')
+ ISNULL(dbo.Addresses.Address + ' ', '') + ISNULL(dbo.Addresses.Suffix + ' ', '') + ISNULL(dbo.Addresses.SubAddress + ' ', '') AS FullAddress, dbo.Addresses.RegionId, dbo.Addresses.CountyId,
dbo.Addresses.OccupancyTypeId, dbo.Addresses.PropertyUseTypeId, dbo.Addresses.Comment, dbo.States.StateAbbr, dbo.States.State, dbo.Regions.Region, dbo.Regions.RegionCode, dbo.Counties.County,
dbo.Counties.CountyCode, dbo.Countries.Country, dbo.OccupancyTypes.OccupancyType, dbo.OccupancyTypes.OccupancyTypeCode, dbo.PropertyUseTypes.PropertyUseType, dbo.Party.PartyName,
dbo.PropertyUseTypes.PropertyUseTypeCode, dbo.UserDefFields.UserDefFieldId, dbo.UserDefFields.FieldDesc, dbo.UserDefValues.UserDefValueId, dbo.UserDefValues.UserDefValue, dbo.Addresses.ZipId, dbo.Zips.Zip,
dbo.AddressParties.PartyID, dbo.Addresses.Latitude, dbo.Addresses.Longitude, dbo.Addresses.Inactive, dbo.Addresses.DefaultPass, dbo.Addresses.Suffix, dbo.AddressTypes.AgencyId, dbo.Addresses.LegalDesc,
dbo.AddressParties.Inactive AS PAInactive, dbo.AddressParties.RoleTypeId, dbo.Addresses.POBox, dbo.AddressParties.ExternalValue, dbo.Addresses.DateUpdated, dbo.Addresses.DateInserted, dbo.Addresses.ReportId,
dbo.Addresses.Map, dbo.Addresses.Block, dbo.Addresses.Lot, dbo.Addresses.TaxParcel, dbo.Addresses.ExternalId, dbo.Addresses.Schedule
FROM dbo.UserDefFields RIGHT OUTER JOIN
dbo.UserDefValues ON dbo.UserDefFields.UserDefFieldId = dbo.UserDefValues.UserDefFieldId RIGHT OUTER JOIN
dbo.Party RIGHT OUTER JOIN
dbo.Addresses LEFT OUTER JOIN
dbo.Zips ON dbo.Addresses.ZipId = dbo.Zips.ZipId LEFT OUTER JOIN
dbo.AddressTypes ON dbo.Addresses.AddressTypeId = dbo.AddressTypes.AddressTypeId LEFT OUTER JOIN
dbo.States ON dbo.Addresses.StateId = dbo.States.StateId LEFT OUTER JOIN
dbo.Regions ON dbo.Addresses.RegionId = dbo.Regions.RegionId LEFT OUTER JOIN
dbo.Counties ON dbo.Addresses.CountyId = dbo.Counties.CountyId LEFT OUTER JOIN
dbo.Countries ON dbo.Addresses.CountryId = dbo.Countries.CountryId LEFT OUTER JOIN
dbo.OccupancyTypes ON dbo.Addresses.OccupancyTypeId = dbo.OccupancyTypes.OccupancyTypeId LEFT OUTER JOIN
dbo.PropertyUseTypes ON dbo.Addresses.PropertyUseTypeId = dbo.PropertyUseTypes.PropertyUseTypeId LEFT OUTER JOIN
dbo.AddressParties ON dbo.Addresses.AddressId = dbo.AddressParties.AddressID ON dbo.Party.PartyID = dbo.AddressParties.PartyID ON dbo.UserDefValues.RecordId = dbo.Addresses.AddressId


Into something like the following:



   public List<AddressPartyList> GetAddressPartyList(Guid? AddressId, Guid? RoleTypeId = null, bool ShowInactive = false, bool FromWebOnly = false, bool WebAcceptedOnly = false, bool WebRejectedOnly = false)
{
IQueryable<AddressPartyList> thisList;

thisList = myappContext.myappData().AddressParties.Join(
myappContext.myappData().RoleTypes, ap => ap.RoleTypeId, rt => rt.RoleTypeId, (ap, rt) => new { ap, rt }).Join(
myappContext.myappData().Parties, apr => apr.ap.PartyId, p => p.PartyId, (apr, p) => new AddressPartyList
{
AddressId = apr.ap.AddressId,
PartyId = apr.ap.PartyId,
AccountId = p.AccountId,
RoleType = apr.rt.RoleType1,
Salutation = p.Salutation,
FirstName = p.FirstName,
MiddleInitial = p.MiddleInitial,
LastName = p.LastName,
Suffix = p.Suffix,
PartyName = p.PartyName,
Email = p.Email,
Comment = p.Comment,
ExternalId = p.ExternalId,
PartyInactive = p.Inactive,
WebAccountId = p.WebAccountId,
DateUpdated = p.DateUpdated,
DateInserted = p.DateInserted,
PriceLevel = p.PriceLevel,
FromWeb = p.FromWeb,
WebAccepted = p.WebAccepted,
WebRejected = p.WebRejected,
RoleTypeId = apr.ap.RoleTypeId,
InactiveAtAddress = apr.ap.Inactive,
IsBus = p.IsBus,
Sequence = apr.ap.Sequence,
AddressPartyId = apr.ap.AddressPartyId

}).Where(p => p.AddressId == AddressId);


if (!ShowInactive)
{
thisList = thisList.Where(p => p.PartyInactive == false && p.InactiveAtAddress == false);
}

if (RoleTypeId != null)
{
thisList = thisList.Where(p => p.RoleTypeId == RoleTypeId);
}

if (FromWebOnly)
{
thisList = thisList.Where(p => p.FromWeb == true);
}
if (WebAcceptedOnly)
{
thisList = thisList.Where(p => p.WebAccepted == true);
}
if (WebRejectedOnly)
{
thisList = thisList.Where(p => p.WebRejected == true);
}

return thisList.GroupBy(p => p.PartyId).Select(p => p.FirstOrDefault()).OrderBy(p => p.Sequence).ThenBy(p => p.PartyName).ToList();
}


Any help would be appreciated. Also, what would be the syntax for a view with multiple right and left outer joins. I didn't code it, I'm just trying to convert it and I understand it's best to use left outer joins.
Thanks.










share|improve this question
















I'm not sure if this is the right forum, if not please move it
My office is in the process of re-writing our app and switching from VB to C# and the entity framework. I've had some success in joining multiple tables all with left outer joins but I'm at my limits on how to convert an sql view like the following:



SELECT        dbo.Addresses.AddressId, dbo.AddressTypes.AddressType, dbo.Addresses.AddressTypeId, dbo.Addresses.ParentAddressId, dbo.Addresses.AddressCode, dbo.Addresses.AddressNumber, dbo.Addresses.Address, 
dbo.Addresses.SubAddress, dbo.Addresses.Direction, dbo.Addresses.City, dbo.Addresses.StateId, dbo.Addresses.CountryId, ISNULL(dbo.Addresses.AddressNumber + ' ', '') + ISNULL(dbo.Addresses.Direction + ' ', '')
+ ISNULL(dbo.Addresses.Address + ' ', '') + ISNULL(dbo.Addresses.Suffix + ' ', '') + ISNULL(dbo.Addresses.SubAddress + ' ', '') AS FullAddress, dbo.Addresses.RegionId, dbo.Addresses.CountyId,
dbo.Addresses.OccupancyTypeId, dbo.Addresses.PropertyUseTypeId, dbo.Addresses.Comment, dbo.States.StateAbbr, dbo.States.State, dbo.Regions.Region, dbo.Regions.RegionCode, dbo.Counties.County,
dbo.Counties.CountyCode, dbo.Countries.Country, dbo.OccupancyTypes.OccupancyType, dbo.OccupancyTypes.OccupancyTypeCode, dbo.PropertyUseTypes.PropertyUseType, dbo.Party.PartyName,
dbo.PropertyUseTypes.PropertyUseTypeCode, dbo.UserDefFields.UserDefFieldId, dbo.UserDefFields.FieldDesc, dbo.UserDefValues.UserDefValueId, dbo.UserDefValues.UserDefValue, dbo.Addresses.ZipId, dbo.Zips.Zip,
dbo.AddressParties.PartyID, dbo.Addresses.Latitude, dbo.Addresses.Longitude, dbo.Addresses.Inactive, dbo.Addresses.DefaultPass, dbo.Addresses.Suffix, dbo.AddressTypes.AgencyId, dbo.Addresses.LegalDesc,
dbo.AddressParties.Inactive AS PAInactive, dbo.AddressParties.RoleTypeId, dbo.Addresses.POBox, dbo.AddressParties.ExternalValue, dbo.Addresses.DateUpdated, dbo.Addresses.DateInserted, dbo.Addresses.ReportId,
dbo.Addresses.Map, dbo.Addresses.Block, dbo.Addresses.Lot, dbo.Addresses.TaxParcel, dbo.Addresses.ExternalId, dbo.Addresses.Schedule
FROM dbo.UserDefFields RIGHT OUTER JOIN
dbo.UserDefValues ON dbo.UserDefFields.UserDefFieldId = dbo.UserDefValues.UserDefFieldId RIGHT OUTER JOIN
dbo.Party RIGHT OUTER JOIN
dbo.Addresses LEFT OUTER JOIN
dbo.Zips ON dbo.Addresses.ZipId = dbo.Zips.ZipId LEFT OUTER JOIN
dbo.AddressTypes ON dbo.Addresses.AddressTypeId = dbo.AddressTypes.AddressTypeId LEFT OUTER JOIN
dbo.States ON dbo.Addresses.StateId = dbo.States.StateId LEFT OUTER JOIN
dbo.Regions ON dbo.Addresses.RegionId = dbo.Regions.RegionId LEFT OUTER JOIN
dbo.Counties ON dbo.Addresses.CountyId = dbo.Counties.CountyId LEFT OUTER JOIN
dbo.Countries ON dbo.Addresses.CountryId = dbo.Countries.CountryId LEFT OUTER JOIN
dbo.OccupancyTypes ON dbo.Addresses.OccupancyTypeId = dbo.OccupancyTypes.OccupancyTypeId LEFT OUTER JOIN
dbo.PropertyUseTypes ON dbo.Addresses.PropertyUseTypeId = dbo.PropertyUseTypes.PropertyUseTypeId LEFT OUTER JOIN
dbo.AddressParties ON dbo.Addresses.AddressId = dbo.AddressParties.AddressID ON dbo.Party.PartyID = dbo.AddressParties.PartyID ON dbo.UserDefValues.RecordId = dbo.Addresses.AddressId


Into something like the following:



   public List<AddressPartyList> GetAddressPartyList(Guid? AddressId, Guid? RoleTypeId = null, bool ShowInactive = false, bool FromWebOnly = false, bool WebAcceptedOnly = false, bool WebRejectedOnly = false)
{
IQueryable<AddressPartyList> thisList;

thisList = myappContext.myappData().AddressParties.Join(
myappContext.myappData().RoleTypes, ap => ap.RoleTypeId, rt => rt.RoleTypeId, (ap, rt) => new { ap, rt }).Join(
myappContext.myappData().Parties, apr => apr.ap.PartyId, p => p.PartyId, (apr, p) => new AddressPartyList
{
AddressId = apr.ap.AddressId,
PartyId = apr.ap.PartyId,
AccountId = p.AccountId,
RoleType = apr.rt.RoleType1,
Salutation = p.Salutation,
FirstName = p.FirstName,
MiddleInitial = p.MiddleInitial,
LastName = p.LastName,
Suffix = p.Suffix,
PartyName = p.PartyName,
Email = p.Email,
Comment = p.Comment,
ExternalId = p.ExternalId,
PartyInactive = p.Inactive,
WebAccountId = p.WebAccountId,
DateUpdated = p.DateUpdated,
DateInserted = p.DateInserted,
PriceLevel = p.PriceLevel,
FromWeb = p.FromWeb,
WebAccepted = p.WebAccepted,
WebRejected = p.WebRejected,
RoleTypeId = apr.ap.RoleTypeId,
InactiveAtAddress = apr.ap.Inactive,
IsBus = p.IsBus,
Sequence = apr.ap.Sequence,
AddressPartyId = apr.ap.AddressPartyId

}).Where(p => p.AddressId == AddressId);


if (!ShowInactive)
{
thisList = thisList.Where(p => p.PartyInactive == false && p.InactiveAtAddress == false);
}

if (RoleTypeId != null)
{
thisList = thisList.Where(p => p.RoleTypeId == RoleTypeId);
}

if (FromWebOnly)
{
thisList = thisList.Where(p => p.FromWeb == true);
}
if (WebAcceptedOnly)
{
thisList = thisList.Where(p => p.WebAccepted == true);
}
if (WebRejectedOnly)
{
thisList = thisList.Where(p => p.WebRejected == true);
}

return thisList.GroupBy(p => p.PartyId).Select(p => p.FirstOrDefault()).OrderBy(p => p.Sequence).ThenBy(p => p.PartyName).ToList();
}


Any help would be appreciated. Also, what would be the syntax for a view with multiple right and left outer joins. I didn't code it, I'm just trying to convert it and I understand it's best to use left outer joins.
Thanks.







linq entity-framework-6 sql-server-2014






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 14:16









Imantas

775415




775415










asked Dec 28 '18 at 13:00









papabearappspapabearapps

1




1













  • First and foremost: don't join, use navigation properties.

    – Gert Arnold
    Dec 28 '18 at 14:56











  • You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

    – NetMage
    Dec 28 '18 at 19:23



















  • First and foremost: don't join, use navigation properties.

    – Gert Arnold
    Dec 28 '18 at 14:56











  • You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

    – NetMage
    Dec 28 '18 at 19:23

















First and foremost: don't join, use navigation properties.

– Gert Arnold
Dec 28 '18 at 14:56





First and foremost: don't join, use navigation properties.

– Gert Arnold
Dec 28 '18 at 14:56













You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

– NetMage
Dec 28 '18 at 19:23





You can translate RIGHT OUTER JOIN to LEFT OUTER JOIN by swapping the left and right hand sides. You can translate RIGHT OUTER JOIN with out an ON condition (which I didn't think was acceptable to SQL Server?) to another from.

– NetMage
Dec 28 '18 at 19:23












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959001%2fconverting-a-sql-server-view-to-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53959001%2fconverting-a-sql-server-view-to-linq%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas