473,473 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create Permutations from Dictionary<string, List<string>>

Hi

I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.

i.e. the dictionary essentially creates the following array:

Key Value

Column1 1
Column1 2
Column2 17

I want to then create an number of filter expressions:

Column1 = 1 AND Column2 = 17
Column1 = 2 AND Column2 = 17

I used a permutation library i found PermuteUtils (
http://www.koders.com/csharp/fid3768...71457A936.aspx
) and the following code:

Dictionary<string, List<string>distinctCellValues = new
Dictionary<string, List<string>>();
ArrayList queryClause = new ArrayList();

{
foreach (KeyValuePair<string, List<string>kvp in
distinctCellValues)
{
foreach (string value in kvp.Value)
{
queryClause.Add(kvp.Key + " = " + value);
}
}

//Convert array to string array
string[] queryClauseString = queryClause.ToArray(typeof(string)) as
string[];

CreateFilterPermutations<string>(queryClauseString ,
distinctCellValues.Count);
}

private void CreateFilterPermutations<T>(IEnumerable<Tinput, int
count)
{
foreach (IEnumerable<Tpermutation in
PermuteUtils.Permute<T>(input, count))
{
foreach (T i in permutation)
{
filterExpressions.Add(i);
}
}
}

This seems to work OK as far as running the CreateFilterPermutations
method, where i just cannot get it to do what i need.

I've manage to get myself very confused by all this, and this only
contains 2 columns. Potentially i could need to be generating
permutations of 10 columns/values or more!! I'm hoping someone can
straighten out my thoughts before my head explodes!!

Thanks
Nov 30 '07 #1
2 5846
There is a good chance I don't understand your problem, but here is my take:

I don't think you want the permutations of the array elements, but instead
want a subset of the Cartesian product of the array with itself. E.G. if
the array is A= [a, b, c], the Cartesian product A x A is
{(a,a), (a,b), (a,c), (b,a), (b,b), (b,c), (c,a), (c,b), (c,c)}. The subset
you want excludes an element whose coords are the same, and considers as
duplicates 2 elements that have the same coords (e.g. (a,b) and (b,a) are
the same).

So, you want {(a,b), (a,c), (b,co)}

If you only have a single array, X, crossed only once with itself,
containing more than one element, some potential pseudo code would be:

for (int i = 0; i < x.Length-1; i++)
{
for (int j = i+1; j < x.Length; j++)
{
myFilterList.Add(x[i].MyToString() + " AND " +
x[j].MyToString())
}
}

In your case, x is the list of KeyValue pairs from the dictionary, and you
need to convert each KeyValue to a string via MyToString(), so as to form a
filter, which you add to a list, myFilterList.

What I don't understand about your example:
You have the same key, Column1 associated with two different values. I
don't think the Dictionary allows this.
"Assimalyst" <c_******@hotmail.comwrote in message
news:8c**********************************@s8g2000p rg.googlegroups.com...
Hi

I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.

i.e. the dictionary essentially creates the following array:

Key Value

Column1 1
Column1 2
Column2 17

I want to then create an number of filter expressions:

Column1 = 1 AND Column2 = 17
Column1 = 2 AND Column2 = 17

I used a permutation library i found PermuteUtils (
http://www.koders.com/csharp/fid3768...71457A936.aspx
) and the following code:

Dictionary<string, List<string>distinctCellValues = new
Dictionary<string, List<string>>();
ArrayList queryClause = new ArrayList();

{
foreach (KeyValuePair<string, List<string>kvp in
distinctCellValues)
{
foreach (string value in kvp.Value)
{
queryClause.Add(kvp.Key + " = " + value);
}
}

//Convert array to string array
string[] queryClauseString = queryClause.ToArray(typeof(string)) as
string[];

CreateFilterPermutations<string>(queryClauseString ,
distinctCellValues.Count);
}

private void CreateFilterPermutations<T>(IEnumerable<Tinput, int
count)
{
foreach (IEnumerable<Tpermutation in
PermuteUtils.Permute<T>(input, count))
{
foreach (T i in permutation)
{
filterExpressions.Add(i);
}
}
}

This seems to work OK as far as running the CreateFilterPermutations
method, where i just cannot get it to do what i need.

I've manage to get myself very confused by all this, and this only
contains 2 columns. Potentially i could need to be generating
permutations of 10 columns/values or more!! I'm hoping someone can
straighten out my thoughts before my head explodes!!

Thanks

Nov 30 '07 #2
Thank you Fred.

I think i had confused myself. Not sure I want permutations but rather
combinations.

Your code sample has given me some ideas that i think i can get
working with a bit of expansion.
I'll keep playing with it . . .

By the way, i think you understood my problem exactly. The bit you
didn't understand was just the way i represented it. I didn't have
duplicate keys it was more like

Column1 { 1, 2 }
Column2 { 17 }

Thanks again
On Nov 30, 6:14 pm, "Fred Mellender" <nospamPlease_fred...@gmail.com>
wrote:
There is a good chance I don't understand your problem, but here is my take:

I don't think you want the permutations of the array elements, but instead
want a subset of the Cartesian product of the array with itself. E.G. if
the array is A= [a, b, c], the Cartesian product A x A is
{(a,a), (a,b), (a,c), (b,a), (b,b), (b,c), (c,a), (c,b), (c,c)}. The subset
you want excludes an element whose coords are the same, and considers as
duplicates 2 elements that have the same coords (e.g. (a,b) and (b,a) are
the same).

So, you want {(a,b), (a,c), (b,co)}

If you only have a single array, X, crossed only once with itself,
containing more than one element, some potential pseudo code would be:

for (int i = 0; i < x.Length-1; i++)
{
for (int j = i+1; j < x.Length; j++)
{
myFilterList.Add(x[i].MyToString() + " AND " +
x[j].MyToString())
}

}

In your case, x is the list of KeyValue pairs from the dictionary, and you
need to convert each KeyValue to a string via MyToString(), so as to form a
filter, which you add to a list, myFilterList.

What I don't understand about your example:
You have the same key, Column1 associated with two different values. I
don't think the Dictionary allows this.

"Assimalyst" <c_oxt...@hotmail.comwrote in message

news:8c**********************************@s8g2000p rg.googlegroups.com...
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the dictionary essentially creates the following array:
Key Value
Column1 1
Column1 2
Column2 17
I want to then create an number of filter expressions:
Column1 = 1 AND Column2 = 17
Column1 = 2 AND Column2 = 17
I used a permutation library i found PermuteUtils (
http://www.koders.com/csharp/fid3768...EE8E638B71457A...
) and the following code:
Dictionary<string, List<string>distinctCellValues = new
Dictionary<string, List<string>>();
ArrayList queryClause = new ArrayList();
{
foreach (KeyValuePair<string, List<string>kvp in
distinctCellValues)
{
foreach (string value in kvp.Value)
{
queryClause.Add(kvp.Key + " = " + value);
}
}
//Convert array to string array
string[] queryClauseString = queryClause.ToArray(typeof(string)) as
string[];
CreateFilterPermutations<string>(queryClauseString ,
distinctCellValues.Count);
}
private void CreateFilterPermutations<T>(IEnumerable<Tinput, int
count)
{
foreach (IEnumerable<Tpermutation in
PermuteUtils.Permute<T>(input, count))
{
foreach (T i in permutation)
{
filterExpressions.Add(i);
}
}
}
This seems to work OK as far as running the CreateFilterPermutations
method, where i just cannot get it to do what i need.
I've manage to get myself very confused by all this, and this only
contains 2 columns. Potentially i could need to be generating
permutations of 10 columns/values or more!! I'm hoping someone can
straighten out my thoughts before my head explodes!!
Thanks
Nov 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joanna Carter \(TeamB\) | last post by:
I would like to copy the contents of a Dictionary<string, object> to another Dictionary<string, object>. Do I have to iterate the source use something like foreach or is there an easier way ? ...
5
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a compilation error:
7
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string,...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
4
by: Mark Rae | last post by:
Hi, Is it possible to create a case-insensitive List<stringcollection? E.g. List<stringMyList = new List<string>; MyList.Add("MyString"); So that:
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
4
by: Maikeru | last post by:
I am trying to create a map that contains a string as the key and a list of strings as the value. Essentially it will be a dictionary where the key is an English word and the list will contain all...
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
2
by: jandeerit | last post by:
hi... can anyone help to show me how to store the array in a dictionary object as shown below? FROM: Array : "PARAMETERS" : "ParamName-1" : "ParamValue-1" : "ParamName-2" ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.