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.

Association with CanBeNull = false produces LeftJoin when using QueryExpressionMethod

See original GitHub issue

LinqToDb-Version: 3.1.0 DB: Firebird 2.1

Hi,

I want to generate an inner join by using the association attribute and a QueryExpressionMethod.

Let’s say I have this structure:

public class Card{
    public int Id { get; set;}
    public string CardName { get; set;}
    [Association(QueryExpression = nameof(Card_Owner), CanBeNull = false)]
    public Client Owner {get; set; }
    public int OwnerId {get; set; }

    public static Expression<Func<Card, IDataContext, IQueryable<Client>>> Card_Owner()
    {
       return (c, db) => db.GetTable<Client>().Where(cl=> cl.Id == c.OwnerId);
    }
}

public class Client{
    public int Id { get; set;}
    public string Name {get; set; }
}

So if I call it :

using(var db = new MyDb()){
    var cards = db.GetTable<Card>().LoadWith(x => x.Client).ToList();
)

The resulting SQL produces a left join.

if I change the Association Attribute to :

public class Card{
    public int Id { get; set;}
    public string CardName { get; set;}
    [Association(ThisKey = "OwnerId", OtherKey = "Id", CanBeNull = false)]
    public Client Owner {get; set; }
    public int OwnerId {get; set; }
}

it produces an inner join. Although both have the CanBeNull set to false!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Nov 4, 2020

Yes, in this case we ignore CanBeNull attribute. Will check what we can do here. But you have to understand if we use INNER JOIN, then association may filter out principal entities.

0reactions
travbeamocommented, Nov 5, 2020

It might not be such an issue if the entities models correctly use Nullable Reference Types for association properties.

If the helper methods & code refactoring proposed in issue https://github.com/linq2db/linq2db/issues/2592 are accepted in to Linq2DB, then the canBeNull attribute could possibly be removed altogether.

Then the correct joins would be used purely based on the if the association properties in the entities model are nullable:

  • If association property is nullable = LEFT join is used
  • If association property is not nullable = INNER join is used
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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