Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# System.Linq.Lookup Class Removing and Adding values

I'm using Lookup class in C# as my prime data container for the user to select values from two Checked List boxes.

The Lookup class is far easier to use than using the class Dictionary>, however I cannot find methods for removing and adding values to a lookup class.

I thought about using the where and the union, but i cant seem to get it right.

Thanks in advance.

like image 875
Ahmad Hajou Avatar asked Oct 27 '10 07:10

Ahmad Hajou


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

Unfortunately the creation of the Lookup class is internal to the .NET framework. The way that the lookup is created, is via static Factory Methods on the Lookup class. These are:

internal static Lookup<TKey, TElement> Create<TSource>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer);
    internal static Lookup<TKey, TElement> CreateForJoin(IEnumerable<TElement> source, Func<TElement, TKey> keySelector, IEqualityComparer<TKey> comparer);

However, these methods are internal and not for consumption by us. The lookup class does not have any way of removing items.

One way you could do an add and remove is to constantly create new ILookups. For example - how to delete an element.

public class MyClass
{
  public string Key { get; set; }
  public string Value { get; set; }
}

//We have a fully populated set:
var set = new List<MyClass>() //Populate this.
var lookup = set.ToLookup(m => m.Key, m => m);

//Remove the item where the key == "KEY";
//Now you can do something like that, modify to your taste.
lookup = lookup
  .Where(l => !String.Equals(l.Key, "KEY"))
   //This just flattens the set - up to you how you want to accomplish this
  .SelectMany(l => l)
  .ToLookup(l => l.Key, l => l.Value);

For adding to the list, we could do something like this:

//We have a fully populated set:
var set = new List<MyClass>() //Populate this.
var lookup = set.ToLookup(m => m.Key, m => m);

var item = new MyClass { Key = "KEY1", Value = "VALUE2" };

//Now let's "add" to the collection creating a new lookup
lookup = lookup
  .SelectMany(l => l)
  .Concat(new[] { item })
  .ToLookup(l => l.Key, l => l.Value);
like image 170
Ray Booysen Avatar answered Sep 17 '22 13:09

Ray Booysen