Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4231986
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:02:17+00:00 2026-05-21T02:02:17+00:00

I have two entities like this: [Table(tblAccount)] public class Account { [Key] [Column(Creditor Registry

  • 0

I have two entities like this:

[Table("tblAccount")]
public class Account
{      
    [Key]
    [Column("Creditor Registry ID", Order = 0)]
    public int CreditRegistryId { get; set; }

    [Key]
    [Required]
    [Column("Account No", Order = 1)] 
    public int AccountNo { get; set; }

    [Column("Minimum Installment")]
    public decimal MinimumInstallment { get; set; }

    [Column("Account Status Date")]
    public DateTime AccountStatusDate { get; set; }

    [Required]
    [Column("Account Type")]
    public string AccountType { get; set; }

    public virtual ICollection<AccountOwner> AccountOwners { get; set; }
}

and

[Table("tblAccountOwner")]
public class AccountOwner
{
    [Key]
    [ForeignKey("Account")]
    [Column("Creditor Registry ID", Order = 0)]   
    public int CreditorRegistryId { get; set; }


    [Key]
    [ForeignKey("Account")]
    [Column("Account No", Order = 1)]           
    public int AccountNo { get; set; }

    [Key]
    [Column("Account Owner Registry ID", Order = 2)] 
    public long AccountOwnerRegistryId { get; set; }

    public virtual Account Account { get; set; }
}

I want to convert following query to work without join as account entity has accountOwners and accounOwner has account navigation property ( primary key/foreign key relation)

var ownerRegId = 731752693037116688L;
var excludeTypes = new[] { 'CA00', 'CA01', 'CA03', 'CA04', 'CA02', 'PA00', 'PA01', 'PA02', 'PA03', 'PA04' };
var maxStateChangeMonth = 4;
var excludeStatusId = 999;
var SumOfMonthlyPayments =
    context.Accounts
           .Join(context.AccountOwners,
                 a => new { CreditorRegistryId = a.CreditRegistryId, a.AccountNo },
                 ao => new { ao.CreditorRegistryId, ao.AccountNo },
                 (a, ao) => new { Account = a, AccountOwner = ao })
           .Where(x => x.AccountOwner.AccountOwnerRegistryID == ownerRegId
                    && !excludeTypes.Contains(x.Account.AccountType)
                    && (x.Account.StateChangeDate == null || x.Account.StateChangeDate.Month - DateTime.Now.Month <= maxStateChangeMonth)
                    && x.Account.AccountStatusID != excludeStatusId)
           .Sum(x => Math.Abs(x.Account.MinimumInstallment));

As Sergi suggested I found this:

var sum = (from account in context.Accounts
           from owner in account.AccountOwners
           where (owner.AccountOwnerRegistryId == ownerRegistryId
               && !excludeTypes.Contains(account.AccountType)
               && (account.StateChangeDate == null ||
                   (account.StateChangeDate.Month - DateTime.Now.Month)
                       <= maxStateChangeMonth)
               && account.AccountStatusId != excludeStatusId
               && (includeMortgage.Contains(account.AccountType) || 
                    account.AccountType.Contains("Mortgage")))
            select account.MinimumInstallment)
               .Sum(minimumInstallment => Math.Abs(minimumInstallment));

I added a an other && clauses

 && (includeMortgage.Contains(account.AccountType) ||
    account.AccountType.Contains("Mortgage") 

but now I get :

The cast to value type 'Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:02:18+00:00Added an answer on May 21, 2026 at 2:02 am

    As you mention, you can use the navigation properties instead of a join, so you could try something like:

    var sum = (from account in context.Accounts
               from owner in account.AccountOwners
               where (owner.AccountOwnerRegistryID == ownerRegId
                   && !excludeTypes.Contains(account.AccountType)
                   && (account.StateChangeDate == null || 
                      (account.StateChangeDate.Month - DateTime.Now.Month)
                           <= maxStateChangeMonth)
                   && account.AccountStatusID != excludeStatusId)
               select account.MinimumInstallment)
                   .Sum(minimumInstallment => Math.Abs(minimumInstallment));
    

    The two “concatenated” from clauses are equivalent to the SelectMany method in Linq.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have two entities with an 1-N relation, like this : table_a ------- id
I have two entities - News and Page. Definition looks like this: /** *
Say I have two Entities. public class Category{ public virtual int Id{get;set;} public virtual
I have two entities say Customer and Order which exist on their own and
I currently have two entities in LLBLGen and would like to merge them together
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
Assume there are two entities with ManyToOne relation: @Entity public class A { private
I have two entities, Group and GroupMember. Group is like it sounds a group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.