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

  • Home
  • SEARCH
  • 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 8282771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:29:25+00:00 2026-06-08T10:29:25+00:00

I have seen this question already but it does not explain it so that

  • 0

I have seen this question already but it does not explain it so that I understand what is actually going on. I’ve been developing for years and never come across this before (though my usage of Linq and Parallel is fairly recent).

My code is:

Parallel.ForEach(databaseMetadata.Rows.Cast<DataRow>(), row => {
        var fieldName = row.Item("Name", "");
        var field = this.Fields.Where(f => f.Name.ToLower() == fieldName.ToLower()).SingleOrDefault();
        if(field != null) { field.Validate(this, connection, row); }
});

Within the field.Validate function it sets a property named ‘HasBeenValidated’ on the field object to true, however, as soon as I come out of this Parallel.ForEach loop, that property is set back to false. Can someone please explain why this is happening and what I can do to ensure that changes within the loops are persisted outside of the loop.

Edited:

Below is a copy of the code in field.Validate:

internal void Validate(EntityAttribute entity, SqlConnection connection, [AllowNull] DataRow metadata) {
    this.HasBeenValidated = true;
    var isRequired = this.IsRequired;
    var maxLength = this.MaxLength;
    var isAutoGenerated = this.IsAutoGenerated;
    var dataType = this.member.PropertyType;
    var dataTypeAsString = "";
    if(metadata != null) {
        isRequired = metadata.Item("IsRequired", false);
        maxLength = metadata.Item("MaxLength", 0);
        isAutoGenerated = metadata.Item("IsAutoGenerated", false);
        dataTypeAsString = metadata.Item("DataType", "");
        if(dataTypeAsString == this.member.PropertyType.ToSqlServerDataType()) { dataTypeAsString = ""; }
    } else {
        dataTypeAsString = this.member.PropertyType.ToSqlServerDataType();
    }
    if(metadata == null || isRequired != this.IsRequired || maxLength != this.MaxLength || isAutoGenerated != this.IsAutoGenerated || dataTypeAsString != "") {
        var sql = string.Format((metadata == null ? "ALTER TABLE [{0}].[{1}] ADD" : "ALTER TABLE {0} ALTER COLUMN"), entity.Schema, entity.Name) + " " + this.Sql + ";";
        if(!connection.ExecuteCommand(sql, 1)) {
            throw new InvalidOperationException("Unable to create or alter column '" + this.Name + "' on table '" + entity.Name + "'.");
        }
    }
}

The HasBeenValidated property is defined on the field object as:

internal bool HasBeenValidated { get; set; }

Thanks in advance.

  • 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-08T10:29:26+00:00Added an answer on June 8, 2026 at 10:29 am

    Apologies if I’ve wasted anyone’s time but I have figured out the cause of this issue. The this.Fields list is an IEnumerable<> of the field type which is queryable. I thought that this would be better than having a greedy list (since this class has a good number of lists on it). The code for generating the Fields list is:

    this.Fields = allProperties
        .Select(property => new { Property = property, Field = property.GetCustomAttributes(typeof(FieldAttribute), true).SingleOrDefault() as FieldAttribute })
        .Where(info => info.Field != null && (info.Field as ListAttribute) == null)
        .Select(info => { info.Field.Member = info.Property; return info.Field; });
    

    What I completely failed to realise was that GetCustomAttributes, rather unexpectedly (from my point of view anyway) regenerates a copy of the attribute class each time it is called.

    Had this been a simpler class I might have suspected this sooner, but I was also changing properties in the fields class when setting the Member property (i.e. extracting metadata from the info.Property and setting properties within the field class based on the properties of that class) so when I was looking at the field class in the debugger I could see a lot of the properties had been changed (which mislead me to think it was the same instance of the field class and not a copy).

    I really apologise if I’ve wasted anyone’s time and effort in this but hopefully by posting my mistake this can help other people in future who might stumble using the GetCustomAttribute in a similar way inside a non-greedy Linq expression.

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

Sidebar

Related Questions

I have seen many answers for this type of question but its not related
This question seems to have been asked a lot, but I haven't seen an
I have seen this question posted here, but always, the answer is that the
I have seen this question that talks about getting the last part of a
In Microsoft SQL Server: I have seen this question several times, but can't see
This question may seem a little bit stackoverflow-implementation specific, but I have seen a
I have seen there is no question that specifically reply to this combination of
For the record, I've already seen this connect item but I can't really understand
So, this question has been asked lots, and i have seen many different answers,
I have seen this question and its answers and they clear up some of

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.