I have this left outer join query
var queryString = (from entity in _dbContext.Links
from translations in _dbContext.LinksTrs.Where(p => p.LinkId == entity.Id).OrderBy(p => p.LanguageCode).Take(1).DefaultIfEmpty()
select new LinksAdminListModel()
{
Id = entity.Id,
Name = (translations == null) ? "" : translations.Name,
Url = entity.Url,
Active = entity.Active
});
As you can see I'm getting just one row from the right side of the query while I return all the left rows.
What I need is, instead sorting just by LanguageCode, I need to tell the query to sort by a specifid LanguageCode first and then the rest.
If I add this:
.OrderBy(p => (p.LanguageCode == currentLanguage) ? 0 : 1)
where currentLanguage is just a string containing the language "en", "fr"... so the query is:
var queryString = (from entity in _dbContext.Links
from translations in _dbContext.LinksTrs.Where(p => p.LinkId == entity.Id).OrderBy(p => (p.LanguageCode == currentLanguage) ? 0 : 1).Take(1).DefaultIfEmpty()
select new LinksAdminListModel()
{
Id = entity.Id,
Name = (translations == null) ? "" : translations.Name,
Url = entity.Url,
Active = entity.Active
});
I get the following error: "Unknown column 'Extent1.Id' in 'where clause'"
I need help since I need to return the current language if it exists, if not the next one.
Thanks.
EDIT
The problem is the combination of the ORDER with the TAKE(1), but I do need to take just the first one...
EDIT 2
The probelm seems to be what I commented above...this is the query I'm using:
var queryString = (from entity in _dbContext.Links
from translations in _dbContext.LinksTrs.Where(p => p.LinkId == entity.Id).OrderByDescending(p => p.LanguageCode == currentLanguage).ThenBy(p => p.LanguageCode).Take(1)
select new LinksAdminListModel()
{
Id = entity.Id,
Name = (translations == null) ? "" : translations.Name,
Url = entity.Url,
Active = entity.Active
});
And this is the SQL it is generating...in case someone can see something in here:
SELECT
`Apply1`.`Id`,
`Apply1`.`Name`,
`Apply1`.`Url`,
`Apply1`.`Active`
FROM (SELECT
`Extent1`.`Id`,
`Extent1`.`OrderPos`,
`Extent1`.`Url`,
`Extent1`.`Active`,
`Extent1`.`DateCreated`,
`Extent1`.`DateModified`,
`Extent1`.`UserIdModified`,
(SELECT
`Project1`.`C1`
FROM (SELECT
CASE WHEN ((`Extent2`.`LanguageCode` = 'es-ES') OR ((`Extent2`.`LanguageCode` IS NULL) AND ('es-ES' IS NULL))) THEN (1) WHEN (NOT ((`Extent2`.`LanguageCode` = 'es-ES') AND ((CASE WHEN (`Extent2`.`LanguageCode` IS NULL) THEN (1) ELSE (0) END) = (CASE WHEN ('es-ES' IS NULL) THEN (1) ELSE (0) END)))) THEN (0) END AS `C1`,
`Extent2`.`Name`,
`Extent2`.`LanguageCode`
FROM `LinkTr` AS `Extent2`
WHERE `Extent2`.`LinkId` = `Extent1`.`Id`) AS `Project1`
ORDER BY
`Project1`.`C1` DESC,
`Project1`.`LanguageCode` ASC LIMIT 1) AS `C1`,
(SELECT
`Project1`.`Name`
FROM (SELECT
CASE WHEN ((`Extent2`.`LanguageCode` = 'es-ES') OR ((`Extent2`.`LanguageCode` IS NULL) AND ('es-ES' IS NULL))) THEN (1) WHEN (NOT ((`Extent2`.`LanguageCode` = 'es-ES') AND ((CASE WHEN (`Extent2`.`LanguageCode` IS NULL) THEN (1) ELSE (0) END) = (CASE WHEN ('es-ES' IS NULL) THEN (1) ELSE (0) END)))) THEN (0) END AS `C1`,
`Extent2`.`Name`,
`Extent2`.`LanguageCode`
FROM `LinkTr` AS `Extent2`
WHERE `Extent2`.`LinkId` = `Extent1`.`Id`) AS `Project1`
ORDER BY
`Project1`.`C1` DESC,
`Project1`.`LanguageCode` ASC LIMIT 1) AS `Name`,
(SELECT
`Project1`.`LanguageCode`
FROM (SELECT
CASE WHEN ((`Extent2`.`LanguageCode` = 'es-ES') OR ((`Extent2`.`LanguageCode` IS NULL) AND ('es-ES' IS NULL))) THEN (1) WHEN (NOT ((`Extent2`.`LanguageCode` = 'es-ES') AND ((CASE WHEN (`Extent2`.`LanguageCode` IS NULL) THEN (1) ELSE (0) END) = (CASE WHEN ('es-ES' IS NULL) THEN (1) ELSE (0) END)))) THEN (0) END AS `C1`,
`Extent2`.`Name`,
`Extent2`.`LanguageCode`
FROM `LinkTr` AS `Extent2`
WHERE `Extent2`.`LinkId` = `Extent1`.`Id`) AS `Project1`
ORDER BY
`Project1`.`C1` DESC,
`Project1`.`LanguageCode` ASC LIMIT 1) AS `LanguageCode`
FROM `Link` AS `Extent1`) AS `Apply1`
EDIT 3
The entities are:
public class Link : EntityBase
{
public int OrderPos { get; set; }
public string Url { get; set; }
public bool Active { get; set; }
public virtual IEnumerable<LinkTr> Translations { get; set; }
}
public class LinkTr : EntityBaseTr
{
public string Name { get; set; }
public string Summary { get; set; }
//[ForeignKey("Link")]
public int LinkId { get; set; }
public virtual Link Link { get; set; }
//[ForeignKey("Language")]
public string LanguageCode { get; set; }
public virtual Language Languages { get; set; }
}
public class EntityBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] //[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public int UserIdModified { get; set; }
}
public class EntityBaseTr
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime? DateCreated { get; set; }
}