0

Sorry for the very rudimentary question.

I used LINQ to join the two tables.

        using(var db = new BooksDbContext())
        {
            var q = db.Books.Join(db.Author,
                b => b.Id,
                a => a.Id,
                (b, a) => new { Book = b, Author = a });
        }

From the obtained result, I would like to fill the null part with 0. I want to leave non-null values ​​as they are.

If you write it without using LINQ, you can imagine this. 0 if null. If it is not null, the value is stored as it is.

        while (reader.Read())
        {
            for(var i = 0; i <= 10; i++)
            {
                if (Convert.IsDBNull(reader[i]))
                {
                    testList[i] = 0;
                }
                else testList[i] = reader[i]
            }
        }

I want to do this with LINQ, but I'm having a hard time thinking about it. I would like advice. Sorry for the boring question.

BTOM
  • 71
  • 7
  • Better to show resulting DTO. But usually it is simple `Field = b.SomeNulable ?? 0` – Svyatoslav Danyliv Aug 11 '21 at 16:39
  • I may be wrong, but my guess is that you want a "full join", i.e. you want to list all the books, including those books that have no author. The list produced by your Linq does not include books that have no author. In Linq there is no direct "full join", but what you could do is make a left join (the Linq you already have), then a right join, and finally "union" the joined list. Have look at https://stackoverflow.com/questions/5489987/linq-full-outer-join – MarcG Aug 12 '21 at 13:53
  • "I would like to fill the null part with 0" - the null part of what? Your example code is very useful since it seems completely unrelated to your LINQ code. Try to make them more comparable, or put some pseudo-code in your LINQ showing what you want to accomplish... – NetMage Aug 12 '21 at 23:39

0 Answers0