List<Document> documents = documentLayer.GetDocuments(username);
List<Document> filteredDocuments = new List<Document>();
string documentName = (string)Session["DocumentName"];
string documentAuthor = (string)Session["DocumentAuthor"];
if ((string.IsNullOrEmpty(documentName)) && (string.IsNullOrEmpty(documentAuthor)))
{
documents.DataSource = documents;
documents.DataBind();
}
else
{
foreach (Document document in documents)
{
string name = document.Name;
string author = document.Author;
if (!(string.IsNullOrEmpty(documentName)) && !(string.IsNullOrEmpty(documentAuthor)))
{
if ((Regex.IsMatch(name, documentName)) && (Regex.IsMatch(author, documentAuthor)))
{
filteredDocuments.Add(document);
}
}
else if (string.IsNullOrEmpty(documentName))
{
if (Regex.IsMatch(author, documentAuthor))
{
filteredDocuments.Add(document);
}
}
else if (string.IsNullOrEmpty(documentAuthor))
{
if (Regex.IsMatch(name, documentName))
{
filteredDocuments.Add(document);
}
}
}
documents.DataSource = filteredDocuments;
documents.DataBind();
}
Refactorings
No refactoring yet !
MattK
November 26, 2008, November 26, 2008 21:23, permalink
Might as well make this easier on yourself with LINQ.
Is there a reason you are using a regex? I threw the below together in LINQPad and it works...
//I just need some values to work with.
var names = new[] { "Tom", "Dick", "Harry", "Hurry","Mary", "Jay" }.AsQueryable();
IEnumerable<Document> documents =
from n in names
select new Document
{
author = n,
name = n.Replace ("a", "").Replace ("e", "").Replace ("i", "").Replace ("o", "").Replace ("u", "")
};
documents.Dump();
System.Random r = new Random();
string documentName = r.Next(2) == 1 ? "Hrry": "";
string documentAuthor = r.Next(2) == 1 ? "Harry":"";
"Name".Dump(documentName);
"Author".Dump(documentAuthor);
//commented out this if else just for demo purposes. On the other hand
//if ((string.IsNullOrEmpty(documentName)) && (string.IsNullOrEmpty(documentAuthor)))
// {
//documents.DataSource = documents;
//documents.DataBind();
// }
// else
//{
//this should be easier to read and more efficient: less accessing of properties, fewer lines,
//fewer function calls, takes advantage of fewer comparisons due to &&
IEnumerable filteredDocuments =
from d in documents
where d.name == (string.IsNullOrEmpty(documentName) ? d.name : documentName)
&& d.author == (string.IsNullOrEmpty(documentAuthor) ? d.author : documentAuthor)
select d;
filteredDocuments.Dump();
//}
}
class Document
{
public string author;
public string name;
Moonshield
November 27, 2008, November 27, 2008 02:15, permalink
Some advices : The way you are using the regular expression is equivalent to using the regular way to do a string comparaison. Also, I would suggest also to filter documents in your document layer by adding the name and author to your GetDocuments(username) method, I would be more efficient as your method can retrieve 500 documents from the database to finally display one or two results for exemple.
List<Document> aDocuments = documentLayer.GetDocuments(username);
string docName = (string)Session["DocumentName"];
string docAuthor = (string)Session["DocumentAuthor"];
bool docNameEmpty = string.IsNullOrEmpty(docName);
bool docAuthorEmpty = string.IsNullOrEmpty(docAuthor);
if (docNameEmpty && docAuthorEmpty)
gridDocuments.DataSource = aDocuments;
else
{
List<Document> aDocumentsFilter = new List<Document>();
aDocuments.ForEach(oDoc =>
{
if ((!docNameEmpty && !docAuthorEmpty && docName == oDoc.Name && docAuthor == oDoc.Author)
|| (docNameEmpty && docAuthor == oDoc.Author)
|| (docAuthorEmpty && docName == oDoc.Name))
aDocumentsFilter.Add(oDoc);
});
gridDocuments.DataSource = aDocumentsFilter;
}
gridDocuments.DataBind();
Have the below code which filters a generic list for certain values. I haven't considered using a predicates to achieve the same effect,
Thanks