Using List<int> array does not work as expected
See original GitHub issueI created a model as below:
public class ApplicationDbEntity
{
...
public int Id { get; set; }
public List<int> GroupIds { get; set; }
}
And while doing some queries I realized that Contains
was not working properly when using GroupIds
field.
The LINQ query looks like this:
dbContext.Applications.Where(x =>
dbContext.UserGroupRoleLinks.Where(u => u.UserId == userId).Select(e => e.GroupId).Any(e => x.GroupIds.Contains(e)) &&
x.Version == dbContext.Applications.Where(a => a.Id == x.Id).Max(a => a.Version))
.Select(app => new ApplicationResponse
{
Id = app.Id,
ApplicationName = app.ApplicationName,
Created = app.Created,
Version = app.Version,
IsPublished = app.IsPublished,
Description = app.Description,
DisplayApplicationName = app.DisplayApplicationName
}).ToListAsync()
And this one result to a query which looks like below:
SELECT a.id AS "Id", a.application_name AS "ApplicationName", a.created AS "Created", a.version AS "Version", a.published AS "IsPublished", a.description AS "Description", a.display_application_name AS "DisplayApplicationName"
FROM applications AS a
WHERE (TRUE = FALSE) AND (a.version = (
SELECT MAX(a0.version)
FROM applications AS a0
WHERE a0.id = a.id))
which is wrong.
After changing the List<int> to int[] started to generate correct queries like below:
SELECT a.id AS "Id", a.application_name AS "ApplicationName", a.created AS "Created", a.version AS "Version", a.published AS "IsPublished", a.description AS "Description", a.display_application_name AS "DisplayApplicationName"
FROM applications AS a
WHERE EXISTS (
SELECT 1
FROM users_user_groups_link AS u
WHERE (u.user_id = @__userId_0) AND u.group_id = ANY (a.groups_id)) AND (a.version = (
SELECT MAX(a0.version)
FROM applications AS a0
WHERE a0.id = a.id))
Had to spend quite some time before I read the documentation in details and to realize that I had to use arrays in c# in order for it to work.
My question is: - is there anyway to throw an exception when using List<T>(when mapping to postgress arrays) instead of generating wrong queries ?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
java - Arrays.asList() not working as it should?
The problem is that you expect autoboxing to work on an array - and it doesn't. In the first case, the compiler autoboxes...
Read more >list - Manual
list () is used to assign a list of variables in one operation. Strings can not be unpacked and list() expressions can not...
Read more >How to convert List<Int> to [Int]?
I have a problem with converting Realm type List< Int > array to Int ... convert value of type 'List' to expected argument...
Read more >Arrays | Elasticsearch Guide [8.9]
Arrays of objects do not work as you would expect: you cannot query each object independently of the other objects in the array....
Read more >C#: IEnumerable, yield return, and lazy evaluation
In the end, I fixed the problem by forcing the iteration to complete with .ToArray() . (There are multiple ways to approach something...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
hi @roji, sorry for the late response, unfortunately now I can not try this because I don’t want to update all projects to the latest. tried it but had so many other dependency issues(not related to entity framework), but whenever this package is stable I will update as soon as possible and check if this is fixed. If u want u can close the ticket for now, and I will reopen if the same problem persists in the latest stable version.
Closing for now, we can revisit later.