SELECT
`Distinct1`.`UserId`,
`Distinct1`.`FirstName`,
`Distinct1`.`LastName`,
`Distinct1`.`EmailAddress`,
FROM ( SELECT DISTINCT
`Extent2`.`UserId`,
`Extent2`.`FirstName`,
`Extent2`.`LastName`,
`Extent2`.`EmailAddress`,
FROM `AssistantTo` AS `Extent1`
INNER JOIN `User` AS `Extent2` ON `Extent1`.`AssistantId` = `Extent2`.`UserId`
INNER JOIN `CustomTagUser` as `Extent3` ON `Extent3`.`UserId` = `Extent2`.`UserId`
WHERE `Extent1`.`OwnerId` = 274 AND `Extent3`.`CustomTagId` = 114
) AS `Distinct1`
This is my Table structure :
I tried using following query but it is giving me error.
var assistants =
dbContext.AssistantsTo
.Include(x => x.Assistant)
.Include(x => x.Assistant.CustomTagUser)
.Where(at =>
at.OwnerId == currentUser.UserId
&& (at.Assistant.CustomTagUser.Count(y => y.CustomTagId == filter) > 0)
)
.Select(at => at.Assistant)
.Distinct()
.ToList();
Error : {"Unknown column 'Extent1.AssistantId' in 'where clause'"}
Basically I have issue with giving filter for
`Extent3`.`CustomTagId` = 114
I think we can use any() but I have bad experience using any() with large data & mysql.
Model classes
public class AssistantTo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AssistantToId { get; set; }
public int AssistantId { get; set; }
[ForeignKey("AssistantId")]
public virtual User Assistant { get; set; }
public int OwnerId { get; set; }
[ForeignKey("OwnerId")]
public virtual User Owner { get; set; }
}
public class CustomTagUser
{
[Key]
public int CustomTagUserId { get; set; }
public int CustomTagId { get; set; }
public int UserId { get; set; }
public DateTime CreatedOn { get; set; }
[ForeignKey("CustomTagId")]
public virtual CustomTags CustomTags { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
[Table("User")]
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
.....
public ICollection<CustomTagUser> CustomTagUser { get; set; }
}
}
Basically I have main problem with applying this part in EF
AND Extent3.CustomTagId = 114
var assistants =
dbContext.AssistantsTo
.Include(x => x.Assistant)
.Include(x => x.Assistant.CustomTagUser)
.Where(at =>
at.OwnerId == currentUser.UserId &&
(
at.Assistant.CustomTagUser.Select(x => x.CustomTagId).Contains(filter)
)
.Select(at => at.Assistant)
.Distinct()
.ToList();
try this one...
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.