question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using List<int> array does not work as expected

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rajmondburgajcommented, Sep 23, 2020

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.

0reactions
rojicommented, Sep 23, 2020

Closing for now, we can revisit later.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found