Text box AutoCompleteSource (winForms) and LINQ query - c#

I have a pre-populated text box with with an AutoCompleteCustomSource:
var source = new AutoCompleteStringCollection();
using(propertiesManagementDataContext db = new propertiesManagementDataContext())
{
var q =
(from t in db.GetTable<Tenant>()
select t.lname + " " + t.fname).ToArray();
source.AddRange(q);
}
txt_callerName.AutoCompleteCustomSource = source;
txt_callerName.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
txt_callerName.AutoCompleteSource = AutoCompleteSource.CustomSource;
It works fine, but I want to know the ID of the auto completed item. How to get it?
I tried like this, but had an error:
var q =
(from t in db.GetTable<Tenant>()
select new {lfname = t.lname + " " + t.fname, t.tenantID}).ToArray();
source.AddRange(q);
//this does not work, because q is not an array of strings anymore
How to autocomplete the textbox and save the connection between Lname, Fname and tenantID of the autocompleted items?

I don't see anything in the AutoCompleteStringCollection class that allows you store extra data for each string ... it's pretty much just a collection of strings.
This isn't ideal, but you could include the ID at the end of the string so it doesn't affect the auto-complete experience while the user is typing, then parse the value back out when they make a selection:
var q =
(from t in db.GetTable<Tenant>()
select string.Format("{0} {1} ({2})", t.lname, t.fname, t.tenantID)).ToArray();
Alternatively, you could use a ComboBox control, which (when configured properly) does everything your TextBox is doing, as well as supports setting the data source to a collection of any class you want (not limited to a collection of strings).
You would then specify the property visible to the user (name) with DisplayMember and then the value of each item (ID) with ValueMember.

You could store all the additional data in parallel Dictionary (or other similar collection)
var q =
(from t in db.GetTable<Tenant>()
select new {lfname = t.lname + " " + t.fname, t.tenantID});
Dictionary<String, Int32> idDictionary = new Dictionary<String, Int32>();
foreach(var item in q)
{
idDictionary.Add(item.lfname, item.tenantID);
source.AddRange(q);
}
...
String currentSuggestion = ...
Int32 id = idDictionary[currentSuggestion];
dbContext.Entities.RemoveById(id);
That is not the ideal solution, because you will have to somehow associate for each textBox with AutoCompletion its own dictionary(either storing it in parallel, or subclassing the TextBox). Also there is possibility that lfname can occur twice in the query.

Related

How do I Get the Children of a DevOps Work Item?

I'm trying to piece together a C# console app that accesses the work items in TFS/DevOps via it's API and compares the original estimate field parent work item with that of all its children and then spits out the name and ID of any work items that do not add up.
So far, I have been able to get back a list of all my work items with the original estimates included, but I still need to get the children of each work item so that I can loop through them and compare the summation of their original estimates with that of their parent. Given how little I know about C# and queries I am pretty stuck right now.
Since linked items are not reportable fields, I have to use $expand to execute a query to get the info I need (at least that's what the doc linked below says). This is where I am stuck. Any tips?
https://learn.microsoft.com/en-us/azure/devops/report/extend-analytics/work-item-links?view=azure-devops
Here is what I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
namespace QueryWorkitems0619
{
class Program
{
static void Main(string[] args)
{
string orgName = "{Organization's name}";
string PAT = "{Personal Access Token}";
Uri uri = new Uri($"https://dev.azure.com/{orgName}");
string project = "Wingnit_2";
VssBasicCredential credentials = new VssBasicCredential("", PAT);
//create a wiql object and build our query
Wiql wiql = new Wiql()
{
Query = "Select * " +
"From WorkItems " +
"Where [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Closed' " +
"And [System.RelatedLinkCount] > '0'" +
"Order By [State] Asc, [Changed Date] Desc"
};
//create instance of work item tracking http client
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
{
//execute the query to get the list of work items in the results
WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;
//some error handling
if (workItemQueryResult.WorkItems.Count() != 0)
{
//need to get the list of our work item ids and put them into an array
List<int> list = new List<int>();
foreach (var item in workItemQueryResult.WorkItems)
{
list.Add(item.Id);
}
int[] arr = list.ToArray();
//build a list of the fields we want to see
string[] fields = new string[5];
fields[0] = "System.Id";
fields[1] = "System.Title";
fields[2] = "System.RelatedLinkCount";
fields[3] = "System.Description";
fields[4] = "Microsoft.VSTS.Scheduling.OriginalEstimate";
//get work items for the ids found in query
var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
//loop though work items and write to console
foreach (var workItem in workItems)
{
foreach (var field in workItem.Fields)
{
Console.WriteLine("- {0}: {1}", field.Key, field.Value);
}
}
Console.ReadLine();
}
}
}
}
}
You are in the right direction, you need to add the expand when you execute the GetWorkItemsAsync method:
var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr, expand: WorkItemExpand.Relations workItemQueryResult.AsOf).Result;
Note: you can't use fields with expand together, so you need to remove it (you will get all the fields).
Loop the results, inside the work item result you will see Relations, check inside the Relations if the Rel is System.LinkTypes.Hierarchy-Forward, if yes - it's a child:
Now, you have the child id inside the Url, extract it and make an API to get his details.

Multiple fields from LINQ to Text Box

how can I pass value from selected field (LINQ) to textbox in winforms?
If single fields, I just do like this
var result = from row in dtValueBranch.AsEnumerable()
where row.Field<int>("branchID") == idBranch
select row.Field<string>("branchName");
StringBuilder sb = new StringBuilder();
foreach (string s in result)
{
sb.Append(s + Environment.NewLine);
}
tbBranch.Text = sb.ToString();
So this is the code LINQ to many fields
var result = from row in dtValueBranch.AsEnumerable()
where row.Field<int>("branchID") == idBranch
select new
{
BranchName = row["branchName"].ToString(),
branchTel = row["branchTel1"].ToString(),
// And many more fields
};
How can I to implement each fields to each textbox?
Keeping in mind that your result query may have multiple rows, your question appears to be focused on how to access the fields of one row. You can do that like this:
var row = result.First(); // select first row of results
textbox1.Value = row.BranchName;
textbox2.Value = row.branchTel;
// etc..
The select new syntax in your query creates an anonymous type in the result enumeration. You can't refer to that type by name, but you can create local variables (like row) if you use the var declaration style and let the compiler do type inferencing.
If you want to cram all the values of one field across all rows into into one textbox (as in your first code sample), you can do something like this:
StringBuilder sbBranch = new StringBuilder();
StringBuilder sbBranchTel = new StringBuilder();
// etc
foreach (var row in result)
{
sbBranch.Append(row.BranchName);
sbBranch.Append(Environment.NewLine);
sbBranchTel.Append(row.branchTel);
sbBranchTel.Append(Environment.NewLine);
// etc
}
tbBranch.Text = sbBranch.ToString();
tbBranchTel.Text = sbBranchTel.ToString();
// etc
However, if you're wanting to display multiple rows of multiple fields of data, you might want to look at using a data grid control instead of a herd of textboxes. You'll save yourself a lot of effort, and your users will probably thank you too.
You can do like this:
var rows = dtValueBranch.AsEnumerable()
.Where(row => row.Field<int>("branchID") == idBranch);
textBox1.Value = string.Join(Environment.NewLine,
rows.Select(row => row.Field<string>("branchName"));
textBox2.Value = string.Join(Environment.NewLine,
rows.Select(row => row.Field<string>("branchTel1"));

Sharepoint list Dynamic Linq query

I need to query a list in SharePoint where the columns may be added in the future.
For instance at the moment I have the following columns
Name, Job, interests, address
I want to be able to query this string dynamically using a parameter from the browser so if columns are added in the future I don’t have to change the code but just the parameter.
The address may look like this www.contoso.com/sites/mypage.aspx?property=Interests
And the code something on the line of this:
var SiteParameter = Request.QueryString["property"];
var ItemsFromList = from item in ListItems where item[try to put the parameter in here] select item;
I use SPmetal to get the list details, so if I press item. Visual Studio2010 will return the columns within the list.
This may be easier without SPMetal.
var qy = new SPQuery();
qy.Query =
"<Where><Eq>" +
"<FieldRef Name=`" + siteParameter + "'/>" +
// You may have to worry about the type of the field here, too.
"<Value Type='Text'>" + desiredValue + "</Value>" +
"</Eq></Where>";
var itemCollection = myList.GetItems(qy);

ASP.NET LINQ SQL get specific fields

I am trying o set textboxes on an ASP.NET webpage using LINQ - SQL. Here is the code I have to perform the select statement:
EQCN = Request.QueryString["EQCN"];
var equipment = from n in db.equipments
where n.EQCN.ToString() == EQCN
select n;
How do I set TextBox1.text to be a specific field in the table?
Thanks so much
EDIT
I need to output every field in the table into different textboxes. So performing a query for ever single one seems a little much. There has to be a way to do this?
Thanks
Well you can select the appropriate field to start with:
EQCN = Request.QueryString["EQCN"];
var values = from n in db.equipments
where n.EQCN.ToString() == EQCN
select n.FieldYouWant;
// Or possibly Single, or First...
var singleValue = values.FirstOrDefault();
I think that's what you were after, but if it's not, please clarify your question.
EDIT: To answer your follow-up, you can use:
EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
where n.EQCN.ToString() == EQCN
select n;
// Or possibly Single, or First...
var entity = query.Single();
textBox1.Text = entity.Name;
textBox2.Text = entity.Description;
textBox3.Text = entity.Title;
// etc
That's assuming you want to have access to everything in the entity. If the entity is very large and you only need a few fields, you might want to do something like this:
EQCN = Request.QueryString["EQCN"];
var query = from n in db.equipments
where n.EQCN.ToString() == EQCN
select new { n.Name, n.Description, n.Title };
// Or possibly Single, or First...
var projection = query.Single();
textBox1.Text = projection.Name;
textBox2.Text = projection.Description;
textBox3.Text = projection.Title;
I'm not sure I'd actually couple the data access and UI layers so closely, but that's a different matter...
You only need to perform the query once, but once that's done, you'll have to assign each field to a TextBox. Start by retrieving only the single item you want:
EQCN = Request.QueryString["EQCN"];
var equipment = (from n in db.equipments
where n.EQCN.ToString() == EQCN
select n).FirstOrDefault();
Then go through and assign each TextBox to the appropriate field:
txtName.Text = equipment.Name;
txtDescription.Text = equipment.Description;
txtValue1.Text = equipment.Value1;
txtValue2.Text = equipment.Value2;
//...
If you have several dozen TextBoxes to assign, you could set up a custom control that can be databound to an equipment object, but even then, you'll still have to write the binding code for your control.
The only way I can think of to totally automate this process is to name each TextBox after a field in your object, then use reflection to match them to values:
var textboxes = Panel1.Controls.OfType<TextBox>();
foreach (TextBox txt in textboxes)
{
string fieldname = txt.ID.Remove(0, 3); //"txtDescription" becomes "Description"
string value = equipment.GetType().GetProperty(fieldname).GetValue(equipment, null) as string;
txt.Text = value;
}

Is there a way to access the columns in a Dapper FastExpando via string or index?

I am pulling in a Dapper FastExpando object and want to be able to reference the column names dynamically at run time rather than at design/compile time. So I want to be able to do the following:
var testdata = conn.Query("select * from Ride Where RiderNum = 21457");
I want to be able to do the following:
foreach( var row in testdata) {
var Value = row["PropertyA"];
}
I understand that I can do:
var Value = row.PropertyA;
but I can't do that since the name of the property i'm going to need won't be known until runtime.
The answer from this SO Question doesn't work. I still get the same Target Invocation exception. So...
Is there any way to do what I want to do with a Dapper FastExpando?
Sure, it is actually way easier than that:
var sql = "select 1 A, 'two' B";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row["A"].IsEqualTo(1);
row["B"].IsEqualTo("two");
Regarding the portion of the title "or index?" - I needed to access results by index since the column names being returned changed sometimes, so you can use a variation of Sam Saffron's answer like this:
var sql = "select 1, 'two'";
var row = (IDictionary<string, object>)connection.Query(sql).First();
row.Values.ElementAt(0).IsEqualTo(1);
row.Values.ElementAt(1).IsEqualTo("two");
There a simple way to access fields direct below sample
string strConexao = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;
conexaoBD = new SqlConnection(strConexao);
conexaoBD.Open();
var result = conexaoBD.Query("Select Field1,Field2 from Table").First();
//access field value result.Field1
//access field value result.Field2
if (result.Field1 == "abc"){ dosomething}

Categories

Resources