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 9016243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:56:42+00:00 2026-06-16T03:56:42+00:00

I have code-first based context with following entities: public class City : IEquatable<City> {

  • 0

I have code-first based context with following entities:

public class City : IEquatable<City>
{

    public City()
    {
        Posts = new List<Post>();
    }

    public City(string cityName) : this()
    {
        Name = cityName;
    }

    public virtual ICollection<Post> Posts { get; private set; }

    public int Id { get; set; }

    public string Name { get; private set; }

    protected string  LoweredName 
    {
        get { return Name.ToLower(CultureInfo.CurrentCulture); }
    }

    public override bool Equals(object obj)
    {
        bool equals = false;
        var city = obj as City;
        if (city != null)
            equals = Equals(city);
        return equals;
    }

    public override int GetHashCode()
    {
        int idHash = Id.GetHashCode();
        int nameHash = LoweredName.GetHashCode();

        var hashCode = idHash ^ nameHash;
        return hashCode;
    }

    public bool Equals(City other)
    {
        return Id == other.Id && LoweredName == other.Name.ToLower(CultureInfo.CurrentCulture);
    }
}
public class Post : IEquatable<Post>
{
    public Post()
    {
        Addresses = new List<PostalAddress>();
    }

    public virtual ICollection<PostalAddress> Addresses { get; private set; }
    public virtual City City { get; set; }

    public int Id { get; set; }

    public string ZipCode { get; set; }
    protected string LoweredZipCode { get { return ZipCode.ToLower(CultureInfo.CurrentCulture); } }

    public bool Equals(Post other)
    {

        return Id == other.Id && City.Equals(other.City) && LoweredZipCode == other.ZipCode.ToLower(CultureInfo.CurrentCulture);
    }
}

DbContext has defined those entities in method OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Configurations.Add(new CityMap());
    modelBuilder.Configurations.Add(new PostMap());
}

public class CityMap : EntityTypeConfiguration<City>
{
    public CityMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        ToTable("City");
        Property(t => t.Id).HasColumnName("Id");
        Property(t => t.Name)
            .HasColumnName("Name")
            .HasMaxLength(450);
    }
}
public class PostMap : EntityTypeConfiguration<Post>
{
    public PostMap()
    {
        // Primary Key
        HasKey(t => t.Id);

        // Properties
        // Table & Column Mappings
        ToTable("Post");
        Property(t => t.Id)
            .HasColumnName("Id");
        Property(t => t.ZipCode)
            .HasColumnName("ZipCode")
            .HasMaxLength(450);

        // Relationships
        HasRequired(t => t.City)
            .WithMany(t => t.Posts)
            .Map(map => map.MapKey("CityId"));
    }
}

I’ve readed some data as POCO object and inserted them into List collection

public class PostImportObject : IEquatable<PostImportObject>
    {

        private string _city;
        private string _loweredCity;

        public string City
        {
            get { return _city; }
            set
            {
                _city = value.CapitalizeFirstLetter();
                _loweredCity = value.ToLower(CultureInfo.CurrentCulture);
            }
        }

        public string ZipCode
        {
            get { return _zipValue; }
            set { _zipValue = value.ToLower(CultureInfo.CurrentCulture); }
        }


        protected string LoweredCity
        {
            get { return _loweredCity; }
        }

        public override bool Equals(object obj)
        {
            bool equals = false;
            var postImport = obj as PostImportObject;
            if (postImport != null)
            {
                equals = Equals(postImport);
            }
            return equals;
        }

        public override int GetHashCode()
        {
            int ziphash = ZipCode.GetHashCode();
            int cityHash = LoweredCity.GetHashCode();

            var hashCode = ziphash ^ cityHash;
            return hashCode;
        }

        public bool Equals(PostImportObject other)
        {
            bool equals = _loweredCity == other.City.ToLower(CultureInfo.CurrentCulture) && ZipCode == other.ZipCode;
            return equals;
        }
    }

If I query data in import list and in database too, my following queries return the same Exception:

using(var db = new DbContext())
{
    var query1 = from post2 in db.Posts.Include("City")
                                    join mergedPost in mergedPosts on new PostImportObject() {City = post2.City.Name, ZipCode = post2.ZipCode} equals new PostImportObject() {City = mergedPost.City, ZipCode = mergedPost.ZipCode} into joinedPosts
                                    from joinedPost in joinedPosts.DefaultIfEmpty()
                                    where joinedPosts==null
                                    select post2;

            var query2= from city1 in db.Cities
                            join postImportObject in mergedPosts on city1.Name equals postImportObject.City
                            join post1 in db.Posts on city1 equals post1.City
                            select post1;
}

I’ll get following exception when querying Any() method of query1 or query2:
Index (zero based) must be greater than or equal to zero and less than the size of the argument list

I’m sorry that I created another topic with same subject, but I didn’t find solution for my problem in other topics.

  • 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-06-16T03:56:43+00:00Added an answer on June 16, 2026 at 3:56 am

    Looking at the stack trace, I’m guessing there’s a problem with the translated version of the ELinq_UnsupportedConstant resource. The English version of this error message is: Unable to create a constant value of type ‘{0}’. Only primitive types (‘{1}’) are supported in this context.

    I think you have two problems:

    1. For a join, the composite key needs to be an anonymous type; you can’t use your PostImportObject as the join key in query1;

    2. You can’t join a database table to a local list;

    I think you’ll need to use .AsEnumerable() to pull the entire list into memory before you can join to the local list:

    var query = from post in context.Posts.Include(p => p.City).AsEnumerable()
                join mergedPost in mergedPosts 
                   on new { City = post.City.Name, post.ZipCode } 
                   equals new { mergedPost.City, mergedPost.ZipCode } 
                   into joinedPosts
               from joinedPost in joinedPosts.DefaultIfEmpty()
               where joinedPost == null
               select post;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Code First approach and have the following Model: public class Person
I have some questions with relationship of ef code first. My code: public class
in Android, I have list that I fill using the following code @Override public
Hi I have the following code: Controller: @Controller public class HelloWorldController { @RequestMapping(/hello) public
I have the following code snippet that builds chained select boxes based on data
I have a library ( based on code found in an old blog post
I have the following types: public abstract class Vehicle { public int Id {
I have the following situation: public abstract class A { private Object superMember; public
I have some code that first selects an option value in a dropdown menu
I have a simple code in Entity Framework (EF) v4.1 code first: PasmISOContext db

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.