Pivot datatable based on key and value in datatable - asp.net

the data i am having is like, i am trying this from week am not able to find any solution.
I need to do this using either pivot using c# or using lists. Kindly provide the answers. immediate.
Key Value
A e
B f
C i
D j
A k
B l
C m
D n
i need something like this
A B C D
-------
e f i j
k l m n
where the first line is a column header.
Any better link of any stackoverflow question also appreciable.
By following code am able to get distinct columns, now problem is with assigning values in rows.
please provide any better way of doing this.
foreach (DataRow row in dtBackupData.Rows)
{
keyname = row["KeyName"].ToString();
if (!keyNameList.Contains(keyname))
{
keyNameList.Add(keyname);
dtDynamic.Columns.Add(keyname, typeof(string));
}
}

Related

How can I print duplicate items from an OrderedDict?

My OrderedDict contains 5 key value pairs which result in the word, "Belle". Every letter its own key with values starting from 1 through 5, from left to right, B = 1, e = 2, etc. Printing this OrderedDict returns only 3 letters, B, l and e. Notice, it doesn't print the repeated e and ls.
In the code below, I use a for statement to print "Belle" so it can print vertically. My goal is to print the complete word "Belle" vertically, with each letter as a key with its value pair.
text6 = OrderedDict({'B':1, 'e':2, 'l':3, 'l':4, 'e':5})
for key, value in text6.items():
print(key, value)
Code above returns:
B 1
e 5
l 4
Desried output:
B 1
e 2
l 3
l 4
e 5
Asking a dict (including OrderedDict) to do what you are asking it to do is impossible. A dict of any type can only contain unique keys. All subsequently added pre-existing keys will overwrite the key and reset it's value.
In order to get the output you want to see, we can use a different data structure. In this implementation, we utilize a list which will contain a tuple for each (value, index) pair we will get by iterating through "Belle" using enumerate
list((v, i) for i, v in enumerate("Belle", 1))

Gremlin : How to get all the vertex details in the traversal?

Sample Data
A knows B
A knows C
A knows D
B knows E
C knows F
Desired Output
B
C
D
E
F
I tried the following query, but it's not working,
g.V('A').
out('knows').
as('x').
out('knows').
as('x').
project('Employee').
by(select('x'))
If you just want to get all the vertices in the path you can do:
g.V('A').repeat(out("knows")).emit().label()
example: https://gremlify.com/c533ij58a98z8

Populating an array using a FOR loop and a function

I was expecting that the following code would populate E with random 1's and 0's, but that does not happen. I cannot figure out why.
Pkg.add("StatsBase")
using StatsBase
function randomSample(items,weights)
sample(items, Weights(weights))
end
n = 10
periods = 100
p = [ones(n,periods)*0.5]
E = fill(NaN, (n,periods))
for i in 1:periods
for ii in 1:n
E(ii,i) = randomSample([1 0],[(p(ii,i)), 1 - p(ii,i)])
end
end
E
The statement:
E(ii,i) = randomSample([1 0],[(p(ii,i)), 1 - p(ii,i)])
defines a local function E and is not an assignment operation to a matrix E. Use
E[ii,i] = randomSample([1, 0],[p[ii,i], 1 - p[ii,i]])
(I have fixed additional errors in your code so please check out the differences)
and for it to run you should also write:
p = ones(n,periods)*0.5

Flattening a graph

I have been given a problem to solve that I am fairly certain its insoluble.
For a system I am working in I need to take piece of branching logic (graph) and translate it to a linear path(flatten it), without node repeats. Given a tree I know that I can do this.
The rules are that the path must be traversed in order, but can 'skip' any panel if some condition is met.
Given the tree:
A > B > C
&&
A > D > E
Our tree can be flattened to:
A > B > C > D > E
So in this case B and C share the same conditional, and D, and E have the inverse of that condition. Thus if B is met so is C, but D and E will be skipped. Conversely, if B is not met, B and C are skipped, but D and E aren't.
So far, so simple. I am fairly convinced this is true for any tree. The problem I have is that the objects I have been given to flatten are graphs, and contain simple cycles, and closed walks.
After that huge preamble my questions are:
Am I right in stating that it is impossible to guarantee that such a graph can be flattened?
I know that closed walks cannot follow my rules (by virtue of returning to a node), but are there any other rules that describe a 'flatten-able' graph versus a 'non-flatten-able' one?
Cheers
If it is a directed graph, then the graph can be flattened if there are no directed cycles.
There is a handy library available JGraphT which will provide the necessary methods to determine if there is a cycle presnet as well a TopologicalOrderIterator which will perform the flattening as well.
Using the graph library and the example above this code will show an exmaple of how to do it.
DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
graph.addVertex("A");
graph.addVertex("B");
graph.addVertex("C");
graph.addVertex("D");
graph.addVertex("E");
// A > B
graph.addEdge("A", "B");
// B > C
graph.addEdge("B", "C");
// A > D
graph.addEdge("A", "D");
// D > E
graph.addEdge("D", "E");
// Uncomment the following line to create a cyclic graph.
//graph.addEdge("E", "D");
CycleDetector<String, DefaultEdge> cycleDector = new CycleDetector<String, DefaultEdge>(graph);
if (cycleDector.detectCycles()) {
System.err.println("Cyclic graph");
} else {
StringBuilder sb = new StringBuilder();
// Create topological order iterator
for (TopologicalOrderIterator<String, DefaultEdge> iter = new TopologicalOrderIterator<String, DefaultEdge>(
graph); iter.hasNext();) {
String vertex = iter.next();
if (sb.length() > 0) {
sb.append(" > ");
}
sb.append(vertex);
}
System.out.println(sb.toString());
}
Sample output with the cycle disabled.
A > B > D > C > E

Wordpress alphabetical index

I have a question.
How can do an alphabetical index with links without plugin ?
Example : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
If a character contains posts, it will be a link.
Else it will text.
Anyone can help me ?
Update : Or something like this http://dribbble.com/tags
You could write a few lines of code to query the database and do a like search on the post titles. The below is not formatted for a WP query, but it should give you a good idea:
SELECT count(*)
FROM wp_posts
WHERE post_title LIKE 'a%';
If the count is greater than 0, you know there are posts and you can add a hyperlink to the letter.

Resources