i am a beginner in programming, i try to search in a list as following. I will try to discribe a bit
... My Class
class TBook
{
public string booktype { get; set; }
public string titel { get; set; }
public string author { get; set; }
public string isbn { get; set; }
public int pages { get; set; }
public TBook(string booktype = "" , string titel = "", string author = "", string isbn = "", int pages = 0)
{
this.booktype = booktype;
this.author = author;
this.isbn = isbn;
this.pages = pages;
this.titel = titel;
}
}
...
.... My List
namespace Bookstore
{
public partial class Bookstore : Form
{
private const string serializeName = "bookstore.dat";
TBookstore booksstore;
List
....
... now my Problem.
as Example, i like to search for all Books of booktype "Standard" containing Math (or something with wildcard like *mat*) in titel. Afterwards i like to return it to an listview.
I tried a lot but nothing works.
Hope you can help me and that i gave enough input.
Regards
Markus
MarkusPosted Jan 11, 2012, 6:34 PM
thanks for respons and help, i need to test.
Data is stored temporary and can be loaded/saved to a binary file.
Hopefully at the weekend i will have a bit time.
Best Wishes
Markus
Jignesh TrivediPosted Jan 11, 2012, 2:12 AM
For searching Wild card charecter use..
public static class LinqExtensions WhereLike( source,> valueSelector,
{
public static IQueryable
this IQueryable
Expression
string value,
char wildcard)
{
return source.Where(BuildLikeExpression(valueSelector, value, wildcard));
}
public static Expression> BuildLikeExpression(> valueSelector,
Expression
string value,
char wildcard)
{
if (valueSelector == null)
throw new ArgumentNullException("valueSelector");
var method = GetLikeMethod(value, wildcard);
value = value.Trim(wildcard);
var body = Expression.Call(valueSelector.Body, method, Expression.Constant(value));
var parameter = valueSelector.Parameters.Single();>(body, parameter);
return Expression.Lambda
}
private static MethodInfo GetLikeMethod(string value, char wildcard)
{
var methodName = "Contains";
var textLength = value.Length;
value = value.TrimEnd(wildcard);
if (textLength > value.Length)
{
methodName = "StartsWith";
textLength = value.Length;
}
value = value.TrimStart(wildcard);
if (textLength > value.Length)
{
methodName = (methodName == "StartsWith") ? "Contains" : "EndsWith";
textLength = value.Length;
}
var stringType = typeof(string);
return stringType.GetMethod(methodName, new Type[] { stringType });
}
}
And Code is ...
var test = books.WhereLike(item => item.Code, firstOptionCode, '*').ToList()
hope this help.
Sam HobbsPosted Jan 10, 2012, 8:52 PM
How is the data saved? Is it in a database table or something else?
If the data is in a database then you can use SQL otherwise you might want to use LINQ.
How much flexibility do you need to provide for the user? Do you need to allow the user to filter using any fields they choose and any data they choose? The more flexibility that you provide your users, the more complicated it will be for you.
Do you need something like the United States of America's Library of Congress Advanced Word Search Form? Or do you need something in which your program specifies the filter but not non-developer users?
The ListView control is older technology. You probably want to use a DataGridView, but that is a relatively minor point.