|
int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 44, 59, 83, 47, 26, 4, 30 };
hmmmm ok,if that,what i should do to get output 44
i want to compare 2 array element ,and if in the same place(which are in same index ) array are equal to show as result
|
|
|
|
|
You can customize your search (intersection) criteria without too much difficulty.
Override IEqualityComparer<T> .
Go through the example given here[^] on msdn.
|
|
|
|
|
This is not very "elegant" code, but I think you may be looking for a solution as simple as this:
int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 44, 59, 83, 47, 26, 4, 30 };
List<int> matches;
private void button1_Click(object sender, EventArgs e)
{
int[] shortAry;
int[] longAry;
matches = new List<int>();
if (id1.Length < id2.Length)
{
shortAry = id1;
longAry = id2;
}
else
{
shortAry = id2;
longAry = id1;
}
for (int i = 0; i < shortAry.Length; i++)
{
if (shortAry[i] == longAry[i]) matches.Add(shortAry[i]);
}
} When you exit this Button ClickEventHandler, then, if matches.Count == 0, there are no matches.
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.
Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
|
|
|
|
|
Try the Zip method[^]:
int[] id1 = { ... };
int[] id2 = { ... };
IEnumerable<int> both = id1.Zip(id2, Tuple.Create).Where(t => t.Item1 == t.Item2).Select(t => t.Item1);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I need save to file User Names from this web: http://worldoftanks.eu/community/clans/500027296-BERU/#wot&mt_order_by=-name
I have tried this with WebClient, WebBrowser, SHDocVw.WebBrowser etc. and nothing work because in downloaded html page code has not filled that informations, what I need. As I undersrand they are showing via AJAX or somethig like this.
WebBrowser has method ShowSaveAsDialog, but I need WITHOUT this dialog
Could someone help me?
P.S. Sorry for my bad english.
|
|
|
|
|
You can use HttpWebRequest to save a file.
Here[^] is an exanple that saves an excel file.
|
|
|
|
|
This also didnt work.
That data are not in the saved document.
|
|
|
|
|
|
|
Nearly every developer worth hiring has written a code generator, some of them even try and turn them into commercial products. 100s of them write articles about their creations and publish them. IMHO every developer should be required to do this.
So take a look at some of the articles already published[^] and see if you can contribute something new or different. Then decide whether you wan to write and article and publish.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
how to access c# with excel please give step by step process
|
|
|
|
|
Excel does not access c# - your question is nonsensical.
Excel may reference a c# library
Excel may be manipulated via VSTO using c#
Excel may be manipulated via interop libraries using c#
Don't you students have teachers to answer this sort of rubbish?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Maybe he is the teacher.
Veni, vidi, abiit domum
|
|
|
|
|
Now there is a frightening thought!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
how can i create a web application using Cassandra and hadoop for database in asp.net with c#
|
|
|
|
|
|
How to use "select" statement in C# with a string array i.e
SqlCommand ("select count(*) from xyZ where ID in <string array="" variable="" name=""> ", con);
|
|
|
|
|
moseslmathew wrote: SqlCommand ("select count(*) from xyZ where ID in <string array="" variable="" name=""> ", con);
Assuming the IDs are numeric then
SqlCommand (Strng.Format("select count(*) from xyZ where ID in ({0})", con);
Assuming the IDs are stringthen
SqlCommand (Strng.Format("select count(*) from xyZ where ID in ('{0}')", con);
Consider researching parameterised queries and "Little Bobby Tables", essential reading.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Try:
string[] ids = { "user1", "user2", "user3" };
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM xyz WHERE id IN " + string.Join(",", ids), con))
{
...
}
The only instant messaging I do involves my middle finger.
English doesn't borrow from other languages.
English follows other languages down dark alleys, knocks them over and goes through their pockets for loose grammar.
|
|
|
|
|
Please don't promote SQL Injection[^]!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You'll need to add each value from the array as a separate parameter:
string[] ids = { "user1", "user2", "user3" };
using (SqlCommand cmd = con.CreateCommand())
{
StringBuilder commandText = new StringBuilder();
IFormatProvider provider = System.Globalization.CultureInfo.InvariantCulture;
commandText.Append("SELECT COUNT(*) FROM xyz WHERE id IN (");
for (int index = 0; index < ids.Length; index++)
{
if (index != 0) commandText.Append(", ");
string name = string.Format(provider, "@P{0:D}", index);
cmd.Parameters.AddWithValue(name, ids[index]);
commandText.Append(name);
}
commandText.Append(')');
cmd.CommandText = commandText.ToString();
...
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hey,
I need some help with a program. The task is: I have 2n points in a list. I want to pair every points in the list by drawing n lines, in such a way that the sum of the lines distances is minimum. After some searches, I tried an implententation with the closest-pair algorithm, but that not really fulfill's the task. The sum can be smaller because of my last pair of "closest-points". So now I kinda have no idea how proceed with this, or what other idea should I consider to implement at all. Here is my implementation (which doesn't fulfill's the task):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Apropiere_Puncte
{
public partial class Form1 : Form
{
List<PointF> points = new List<PointF>();
public Form1()
{
InitializeComponent();
Random r = new Random();
for (int i = 0; i < 20; i++)
{
PointF p = new PointF(r.Next() % this.Size.Width - 20, r.Next() % this.Size.Height - 20);
if (p.X < 20)
p.X = 20;
if (p.Y < 20)
p.Y = 20;
points.Add(p);
}
}
public class Segment
{
public Segment(PointF p1, PointF p2)
{
P1 = p1;
P2 = p2;
}
public readonly PointF P1;
public readonly PointF P2;
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
public float LengthSquared()
{
return (P1.X - P2.X) * (P1.X - P2.X) + (P1.Y - P2.Y) * (P1.Y - P2.Y);
}
}
public static Segment Closest_BruteForce(List<PointF> points)
{
int n = points.Count;
var result = Enumerable.Range(0, n - 1)
.SelectMany(i => Enumerable.Range(i + 1, n - (i + 1))
.Select(j => new Segment(points[i], points[j])))
.OrderBy(seg => seg.LengthSquared())
.First();
return result;
}
public static Segment MyClosestDivide(List<PointF> points)
{
return MyClosestRec(points.OrderBy(p => p.X).ToList());
}
private static Segment MyClosestRec(List<PointF> pointsByX)
{
int count = pointsByX.Count;
if (count <= 4)
return Closest_BruteForce(pointsByX);
var leftByX = pointsByX.Take(count / 2).ToList();
var leftResult = MyClosestRec(leftByX);
var rightByX = pointsByX.Skip(count / 2).ToList();
var rightResult = MyClosestRec(rightByX);
var result = rightResult.Length() < leftResult.Length() ? rightResult : leftResult;
var midX = leftByX.Last().X;
var bandWidth = result.Length();
var inBandByX = pointsByX.Where(p => Math.Abs(midX - p.X) <= bandWidth);
var inBandByY = inBandByX.OrderBy(p => p.Y).ToArray();
int iLast = inBandByY.Length - 1;
for (int i = 0; i < iLast; i++)
{
var pLower = inBandByY[i];
for (int j = i + 1; j <= iLast; j++)
{
var pUpper = inBandByY[j];
if ((pUpper.Y - pLower.Y) >= result.Length())
break;
Segment segment = new Segment(pLower, pUpper);
if (segment.Length() < result.Length())
result = segment;
}
}
return result;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
foreach (PointF p in points)
{
e.Graphics.DrawEllipse(new Pen(Color.Black), p.X - 1, p.Y - 1, 2, 2);
}
while (points.Count >= 2)
{
var result2 = MyClosestDivide(points);
e.Graphics.DrawLine(new Pen(Color.Green), result2.P1, result2.P2);
points.Remove(result2.P1);
points.Remove(result2.P2);
}
}
}
}
Sorry for my english and thanks for help,
Best regards
|
|
|
|
|
It looks like most of your code comes from RosettaCode.com: [^]; nothing wrong with that, although, it's generally a good thing to indicate the source of your code, since it might help people reading your post understand the context of your problem.
I am not clear what you are trying to do: are you attempting to take every point in the List of Points (except the last two which are identical ?), find the closest point to each of them (which would then give you a collection of pairs of points of approximately size = size-of-List / 2 ?
Please describe your goals in more detail.
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.
Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
|
|
|
|
|
I have a Visio ER diagram and want to read the database properties (Columns, Primary, Foreign key, data type) information from an Entity. Also want to find the parent and child tables associated. How can I programmatically achieve it using C#?
I am using Interop Visio library and can read the pages and shape from ER diagram but don't know which functions or methods in Visio interop will let me get properties information from a Shape.
Below is the code I am using and I am not getting any property using it. My ER diagram have just two entities a Parent and a Child table.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Visio;
namespace Visio_POC
{
public partial class Load_Visio : Form
{
static string strProperties = "";
public Load_Visio()
{
InitializeComponent();
string strFileName = "\\Visio_POC\\POC_Visio.vsd";
Microsoft.Office.Interop.Visio.Application vsApp = new Microsoft.Office.Interop.Visio.Application();
Microsoft.Office.Interop.Visio.Document docVisio = vsApp.Documents.Add(strFileName);
Page pgVisio = docVisio.Pages[1];
printProperties(pgVisio.Shapes);
txtProperties.Text = strProperties;
}
public static void printProperties(Shapes shapes)
{
// Look at each shape in the collection.
foreach (Shape shape in shapes)
{
// Use this index to look at each row in the properties
// section.
short iRow = (short) VisRowIndices.visRowFirst;
// While there are stil rows to look at.
while (shape.get_CellsSRCExists(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsValue,
(short) 0) != 0)
{
// Get the label and value of the current property.
string label = shape.get_CellsSRC(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsLabel
).get_ResultStr(VisUnitCodes.visNoCast);
string value = shape.get_CellsSRC(
(short) VisSectionIndices.visSectionProp,
iRow,
(short) VisCellIndices.visCustPropsValue
).get_ResultStr(VisUnitCodes.visNoCast);
// Print the results.
//Console.WriteLine(string.Format(
// "Shape={0} Label={1} Value={2}",
// shape.Name, label, value));
strProperties = strProperties + shape.Name + " - " + label + " - " + value;
// Move to the next row in the properties section.
iRow++;
}
// Now look at child shapes in the collection.
if (shape.Master == null && shape.Shapes.Count > 0)
printProperties(shape.Shapes);
}
}
}
}
|
|
|
|
|
Hi,
I am relatively new to c# and I am trying to utilize a background worker to run the progressBar however I am not getting beyond 1% in the following example. In the Form1_Load I have an iteration inside which I want to display the ProgressBar. Appreciate if someone can point out where I am going wrong with this logic.
Pat
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
BackgroundWorker bgw = new BackgroundWorker();
float tot1=100;
float cnt1=0;
float rslt = 0;
int percents = 0;
public Form1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
{
System.Threading.Thread.Sleep(500);
rslt = ((cnt1 / tot1) * 100);
percents = Convert.ToInt32(rslt);
bgw.ReportProgress(percents, cnt1);
}
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
label2.Text = String.Format("Total items transfered: {0}", e.UserState);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do the code when bgv completes its work
}
private void Form1_Load(object sender, EventArgs e)
{
for (cnt1 = 0; cnt1 <= tot1; cnt1++) //some number (total)
{
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.WorkerReportsProgress = true;
if (!bgw.IsBusy)
{
bgw.RunWorkerAsync();
}
}
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|