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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:03:31+00:00 2026-06-12T21:03:31+00:00

my issue is that I have an attribute as ‘attribute’ coming in from entity

  • 0

my issue is that I have an attribute as ‘attribute’ coming in from entity framework.
So I retrieve this object which has a list of attribute tags, they are accessible via attribute.AttributeTags. Now I have a asp:TextBox where users can edit, remove and add new tags (comma separated). (On page load I am adding the attribute tags to this TextBox)

After a postback on the page I am returning the user input and splitting it into an array of strings and storing it in a variable called AttributeTags.

Now, I would like to add new tags that are not contained in the original attributes list coming from EF and would like to remove the ones that are contained in attributes but not found in the user input string array AttributeTags.

I am doing something like this:

        BusinessObjects.Attribute attribute = db.Attributes.FirstOrDefault(a => a.attribute_id == AttributeID);
        string[] AttributeTags = txtAttributeTags.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in AttributeTags)
        {
            if (!attribute.AttributeTags.Any(t => t.value == item))
            {
                AttributeTag tag = new AttributeTag { value = item, timestamp = DateTime.Now };
                attribute.AttributeTags.Add(tag);
            }
            else
            {
                AttributeTag tag = attribute.AttributeTags.FirstOrDefault(t => t.value == item);
            }
        }

But I’m sort of stuck here since i’m fairly new to LINQ and EF.

  • 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-12T21:03:33+00:00Added an answer on June 12, 2026 at 9:03 pm

    I have two solution of this situations.


    First Solution

    We can create an ExcepWith method that will allow us to remove all the items in a ICollection<T> that are already in give a IEnumerable<T>. The code for such method follows:

    public static int ExceptWith<TItem>
    (
        this ICollection<TItem> collection,
        IEnumerable<TItem> other
    )
    {
        if (ReferenceEquals(collection, null))
        {
            throw new ArgumentNullException("collection");
        }
        else if (ReferenceEquals(other, null))
        {
            throw new ArgumentNullException("other");
        }
        else
        {
            int count = 0;
            foreach (var item in other)
            {
                while (collection.Remove(item))
                {
                    count++;
                }
            }
            return count;
        }
    }
    

    Now you have an string[] with the input of the user, that array is an IEnumerable<string> but not an an ICollection<string>… that is easily solved as follows:

    Instead of this:

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    

    You do this:

    var AttributeTags =
        new List<string>
        (
            txtAttributeTags.Text.Split
            (
                new string[] { "," },
                StringSplitOptions.RemoveEmptyEntries
            )
        );
    

    Or even this:

    var AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        ).ToList();
    

    Now you can do this:

    AttriuteTags.ExceptWith(existingTags);
    

    Since the type of attribute.AttributeTag is not IEnumerable<string> you use Select:

    AttriuteTags.ExceptWith(attribute.AttributeTag.Select(item => item.value));
    

    And that leaves only the new tags in the list.


    Note: this method depends on the implementation of Remove, if you need to do an special comparison, then you are out of luck with this method.


    Second Solution

    There is another way. You can use the Except from the Enumerable class.

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    var newTags = AttributeTags.Except(existingTags);
    

    Since the type of attribute.AttributeTag is not IEnumerable<string> you use Select:

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    var newTags = AttributeTags.Except
    (
        attribute.AttributeTag.Select(item => item.value)
    );
    

    And that puts in newTags, well, the new tags.


    Note: If you need to do an special comparison, then you should use the other overload of the method:

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    var newTags = AttributeTags.Except(attribute.AttributeTag, equalityComparer);
    

    Sadly the equalityComparer is an object of a class that implements IEqualityComparer, meaning that you can’t use lambdas there. For that you can add this class:

    public class CustomEqualityComparer<T> : IEqualityComparer<T>
    {
        private Func<T, T, bool> _comparison;
        private Func<T, int> _getHashCode;
    
        public CustomEqualityComparer
        (
            Func<T, T, bool> comparison,
            Func<T, int> getHashCode
        )
        {
            if (ReferenceEquals(comparison, null))
            {
                throw new ArgumentNullException("comparison");
            }
            else if (ReferenceEquals(getHashCode, null))
            {
                throw new ArgumentNullException("getHashCode");
            }
            else
            {
               _comparison = comparison;
               _getHashCode = getHashCode;
            }
        }
    
        public bool Equals(T x, T y)
        {
            return _comparison.Invoke(x, y);
        }
    
        public int GetHashCode(T obj)
        {
            return _getHashCode.Invoke(obj);
        }
    }
    

    And now invoke like this (for example):

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    var newTags = AttributeTags.Except
    (
        existingTags,
        new CustomEqualityComparer<string>
        (
            (a, b) => 1, //your custom comparison here
            str => str.GetHashCode()
        )
    );
    

    Since the type of attribute.AttributeTag is not IEnumerable<string> you use Select:

    string[] AttributeTags =
        txtAttributeTags.Text.Split
        (
            new string[] { "," },
            StringSplitOptions.RemoveEmptyEntries
        );
    var newTags = AttributeTags.Except
    (
        attribute.AttributeTag.Select(item => item.value),
        new CustomEqualityComparer<string>
        (
            (a, b) => 1, //your custom comparison here
            str => str.GetHashCode()
        )
    );
    

    Adding the new tags

    Now that you have the new tags, let’s say in newTags, you can iterate it to add the new tags:

    var now = DateTime.Now;
    foreach (var item in newTags)
    {
        AttributeTag tag = new AttributeTag { value = item, timestamp = now };
        attribute.AttributeTags.Add(tag);
    }
    

    Comparing the Solutions

    What’s the difference of these methods?

    • The first requires less memory
    • The first requires to define a new method.
    • The first doesn’t allow for a custom IEqualityComparer<T>
    • The second allows for deferred execution.
    • The second uses (not needed) a helper class.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a domain object that has an attribute which is a collection containing
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
I have an ERD with a main table (A) which has one attribute(String) that
I have a pretty common layout issue that I have traditionally used a table
My issue is that I have two (or more) divs of the same class,
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
My issue is that I have created a table for a local db in
Having an issue here that I have tried everything I can think of but
I'm running into an issue in that I have a document indexed with elasticsearch
I'm trying to learn html and jquery. My issue is that I have box

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.