Using List of (T) with Lucene.net in vb.net - asp.net

i want to use an List to store the title, path,... from Documents.
I declared the list like this:
Dim MyDocuments As New List(Of Document)
But i don't really know how to handle the list.
i want to use the list instead of an ReDim Array.
For i = 0 To results - 1 Step 1 ' forschleife zum durchlaufen der Ergebnisse
Try
MyDocuments.Add(New Document())
array_results(i, 0) = hits.Doc(i).Get("title")
array_results(i, 0) += hits.Doc(i).Get("doc_typ")
array_results(i, 1) = hits.Doc(i).Get("pfad")
'array_results(i, 2) = hits.Doc(i).Get("date_of_create") '
array_results(i, 2) = hits.Doc(i).Get("last_change")
array_results(i, 3) = CStr(hits.Score(i))
array_results(i, 4) = hits.Doc(i).Get("doc_typ")
Can I store the object Document, or do i have to create an own class??
Is there a good tutorial for using the list? (i searched, but didn't found something good)
Is the List of (T) the right data structure?
but how can i do like mylist(i) ->gettitle() or something like this?
thanks in advance!

Yes, you can store your documents in a generic List. Deciding if a List<T> is the right data structure or not depends on what you want to do with it. Maybe if you provide more information someone could come up with a better example. I don't know VB.NET so i'll do it in C#.
// i assume you're using the Document class of Lucene.NET
List<Document> documents = new List<Document>();
// add the documents to your collection
for (i = 0; i < hits.Length(); i++)
{
// each result in the list contains a Document
// which you can add to your list
documents.Add(hits.Doc(i));
}
// you can search the list for a Document following a specific rule, using lambda expressions
Document myDoc = documents.Find(d => d.Get("title") == "a value");
// you can get a document by a specific index
Document myOtherDoc = documents[0];
// you can search the list for multiple Documents following a specific rule, using lambda expressions
List<Document> myDocs = documents.FindAll(d => d.Get("doc_typ") == "a type");
More information about the List<T> can be found here: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
More information on lambda expressions can be found here: http://msdn.microsoft.com/en-us/library/bb397687.aspx
This article on SO shows how to use lambdas to search a List<T>

Related

How to Advance filter for category and author in Smartsearch in kentico

I have created basic search and uses the SearchHelper to get smart search results based on the search paramaters.
Now creating the Advance search based on Category , Author etc but did not find the way to filter the result based on these condition.
I am looking for a way to display the results using the dataset that
// Prepare parameters
SearchParameters parameters = new SearchParameters()
{
SearchFor = searchText,
SearchSort = SearchHelper.GetSort(srt),
Path = path,
ClassNames = DocumentTypes,
CurrentCulture = culture,
DefaultCulture = defaultCulture,
CombineWithDefaultCulture = CombineWithDefaultCulture,
CheckPermissions = CheckPermissions,
SearchInAttachments = SearchInAttachments,
User = (UserInfo)CMSContext.CurrentUser,
SearchIndexes = Indexes,
StartingPosition = startPosition,
DisplayResults = displayResults,
NumberOfProcessedResults = numberOfProceeded,
NumberOfResults = 0,
AttachmentWhere = AttachmentsWhere,
AttachmentOrderBy = AttachmentsOrderBy,
BlockFieldOnlySearch = BlockFieldOnlySearch,
};
// Search
DataSet results = SearchHelper.Search(parameters);
The easiest way is to use the method:
SearchHelper.CombineSearchCondition()
The first parameter is the searchText, with the search terms you probably already have.
The second parameter is searchConditions, which can be formatted as per https://docs.kentico.com/k10/configuring-kentico/setting-up-search-on-your-website/smart-search-syntax
Alternatively you could just append your search conditions to your search text manually, separating each term with a space.
Remember that to filter based on any field they need to be selected as searchable in the SiteManager->Development->DocumentTypes->DocumentType->Search Tab.

Filtering a Backbone.js collection by index

I have Backbone.js collection that holds (for example) 30 items.
I want to pass to my template filtered collection consist of every 3rd item in the original collection.
Does anyone know how it can be done elegantly? CoffeeScript code is preferred.
Assuming here that originalCollection is your existing collection
var newCollection = new Backbone.Collection();
for (var i = 0, l = originalCollection.length; i < l; i++) {
if (i % 3 === 0) { newCollection.add(originalCollection.models[i]); }
}
This code works by looping through each existing model, and only adding the model the new collection if it's index is a multiple of 3.
You could make this a little nicer, by using the underscore each method exposed by Underscore.js in Backbone Collections:
var newCollection = new Backbone.Collection();
originalCollection.each(function (model, index) {
if (index % 3 === 0) { newCollection.add(model); }
});
Converting the above to CoffeeScript results in:
newCollection = new Backbone.Collection()
originalCollection.each (model, index) ->
newCollection.add model if index % 3 is 0
Backbone collection have some useful Underscore methods mixed-in. You can use filter to get an array of models that you can pass to the template:
filteredModels = collection.filter (model, i) -> i % 3 == 0
Alternatively, you can use an array comprehension; though I think this is less readable...
filteredModels = (model for model, i in collection.models when i % 3 == 0)
If you really need a Backbone.Collection in your template, you can create a new one with those filtered models:
filteredCollection = new Backbone.Collection filteredModels
Here is a working jsfiddle example.

Convert SQL to LINQ query to get max

hi guys i am stuck converting below sql to LINQ query.
all i want is to have maximum number from list of (FA-00001 ,FA-00059)
SELECT MAX(CAST(SUBSTRING(ReferenceId, PATINDEX('%-%', ReferenceId) + 1, LEN(ReferenceId) - PATINDEX('%-%', ReferenceId)) AS int)) AS MaxReferenceId FROM [ClientRC].[dbo].[EHO_Action]
is this possible to convert to LINQ? thanks
An alternative approach using anonymous projection:
var y = (from record in
(from record in db.ClientRC
select new
{
Group = "x",
ReferenceNumber = Convert.ToInt32(record.ReferenceId.Split('-')[1])
})
group record by new { record.Group } into g
select new
{
MaxReferenceId = g.Max(p => p.ReferenceNumber)
});
http://msdn.microsoft.com/en-us/library/bb386972.aspx
var myvar = (from v in db.object where v!=null select v.id).Max();
MSDN has lots of examples for stuff like this.
Or, you can execute queries directly against a datacontext if you're using entity framework. Just make sure if you're doing anything with parameters you're parameterizing the query and not taking user input directly into it.
http://msdn.microsoft.com/en-us/library/ee358769.aspx
Try this..
var list = DBContext.EHO_Action
.Where(x => x.YourListColumn != null)
.Select(x => x.YourListColumn).ToList(); // Take the list (FA-00001 ,FA-00059) from db to a list
var maxNo = list.Max(x => Convert.ToInt32(x.Split('-')[1]));
Please change the context and column names according to your Linq context.
If you want to use sql you can do it this way..
var list = DBContext.ExecuteQuery<string>("select yourreqrdcolumn from [ClientRC].[dbo].[EHO_Action]");

Regarding to retrieve values inside the array

Hi
I am creating online quiz in asp.net c#. For that i have one form that displays testlist in dropdownlist & start button. After clicking 2nd form appears, 2nd form shows one label for question, radiobuttonlist for answers ,next & checkbox for review. I am creating array of random question ids in start button click event of the 1stform. when i click next button in 2nd form then next random question appears, i want array of questions those are checked for review. I used code for arrays of values ( eg.10101) 1 for true & 0 for false as follows but i want array of that question ids those are checked:
int[] a = (int[])Session["values"];//this is array of random question ids created in 1st form
int g;
if (chkmark.Checked == true)
{
g = 1;
}
else
{
g = 0;
}
int[] chkarray = new int[Convert.ToInt32(Session["Counter"]) - 1];
int[] temp1 = (int[])Session["arrofchk"];
int k, no;
if (temp1 == null)
no = 0;
else
no = temp.Length;
for (k = 0; k < no; k++)
{
chkarray[k] = temp1[k];
}
chkarray[j] = g;
Personally, i would use a Dictionary<int, bool> for this.
In the key of the dictionary, you can store the random Question ID, in the value of the pair, you can store the checked item state. It might take you more work now to refactor it, but I believe it will save you a lot of time in the end when you want to do more actions on your quiz items.
Using a dictionary - or at least a well chosen collection, I think it will be easier to get the right data back.
For your example, it can work only if the positions of both arrays are the same.
Dictionary<int, bool> quizAnswers = new Dictionary<int, bool>(); // <questionID, checked>
// Fill dictionary with questions and answers
for(int i=0;i<a.length;i++)
{
if(temp1.length > i) // Make sure we don't get an IndexOutOfBoundsException
{
quizAnswers.Add(a[i], temp1[i] == 1);
}
}
// Get Answered question in array ( LINQ )
int[] checkedAnswers = (from KeyValuePair<int, bool> pair in quizAnswers
where pair.Value == true
select pair.Key).ToArray<int>();
The reason I am using a Dictionary here, is because I personally think it's neater than having two separate arrays.
I believe you should implement a Dictionary in your quiz, in stead of those arrays. What if the array indexes don't match, or you want to dynamically add a question to a fixed size array, etc..
It's something to take into consideration. Hope I could help you out.

dynamically build Intersect statement ASP.NET

I would like to use the IEnumerable function Intersect() to combine a few list and get the similar integers from each list. The problem I'm faced with is that I don't know how many list I will need to compare.
Here is an example:
A{1,2,3,4}
B{1,2,3}
C{1,2}
results = A.Intersect(B).Intersect(C)
This works great, but the next time around I may have a D{1,2} next time I come across the function.
I'd like to use the Intersect method, but I'm open to new ideas as well.
If you are receivng the collections in a list, you could do this:
List<List<int>> lists = new List<List<int>>();
var result = lists[0].AsEnumerable();
for (int i = 0; i < lists.Count - 1; i++)
{
result = result.Intersect(lists[i + 1]);
}

Resources