Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am having following problem

I wrote two filters to data grid view as follows
public void FilterFromBodyContent(CheckedListBox obj)
        {
            List<unreademails> newList = new List<unreademails>();
            List<unreademails> list = (List<unreademails>)dgvUnreadMails.DataSource;
            List<unreademails> filteredList = (List<unreademails>)dgvUnreadMails.DataSource;
            List<string> words = new List<string>();
            foreach (object o in obj.CheckedItems)
            {
                string s = o.ToString().ToLower();
                if (s != null)
                {
                    string[] separated = s.Split(',');
                    foreach (string word in separated)
                    {
                        if (!string.IsNullOrEmpty(word))
                        {
                            if (!words.Contains(word))
                            {
                                words.Add(word);
                            }
                        }
                    }
                }
            }

            foreach (string item in words)
            {

                filteredList = (List<unreademails>)(from ci in list
                                                    where ci.BodyContent.ToLower().Contains(item)

                                                    select ci).ToList<unreademails>();

                newList = newList.Union(filteredList).ToList();
            }

            dgvUnreadMails.DataSource = newList;
            btnCancel.Enabled = true;
        }</unreademails></unreademails></string></string></unreademails></unreademails></unreademails></unreademails></unreademails></unreademails>
then i wort dynamic counter for get number of words in paticutler cell as follows
 private object GetDynamicDataTable(List<unreademails> emailList, List<string> words)
        {
            DataTable dtList = new DataTable();
            dtList.Columns.Add("Sender Name");
            dtList.Columns.Add("Sender Address");
            dtList.Columns.Add("Date Time");
            dtList.Columns.Add("Subject");
            dtList.Columns.Add("Body Content");
            dtList.Columns.Add("Attachments");
            dtList.Columns.Add("EntryID");

            foreach (string dynamic in words)
            {
                dtList.Columns.Add(dynamic);
            }

            foreach (UnreadEmails mail in emailList)
            {
                DataRow dr = dtList.NewRow();
                dr["Sender Name"] = mail.SenderName;
                dr["Sender Address"] = mail.SenderAddress;
                dr["Date Time"] = mail.RecivedTime;
                dr["Body Content"] = mail.BodyContent;
                dr["Subject"] = mail.Subject;
                dr["EntryID"] = mail.EntryID;



                foreach (string item in words)
                {
                    object count = GetWordCount(mail.BodyContent.ToString(), item);
                    //if (count.ToString != null)
                    //{
                        dr[item] = count;
                    //}
                   // dr[item] = GetWordCount(mail.BodyContent.ToString(), item);
                }

                dtList.Rows.Add(dr);
            }                      
            return dtList;
        }

        private object GetWordCount(string p, string item)
        {
            string[] source = p.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] arrremove = null;
            int count = 0;            
            frmStopWordList stopWords = new frmStopWordList();
            arrremove = stopWords.getStopWords();

            for (int i = 0; i < source.Length; i++)
            {

                for (int j = 0; j < arrremove.Length; j++)
                {
                    if (source[i] == arrremove[j])
                    {
                        source[i] = "";
                    }                  
                }
            }

            var matchQuery = from word in source
                             where word.ToLowerInvariant() == item.ToLowerInvariant()
                             select word;

            count = matchQuery.Count();

            return count;            
        }
</string></unreademails>



but when i run filter it is throw an exception telling "Unable to cast object of type 'System.Data.DataTable' to type 'System.Collections.Generic.List`1[E_mail_Filter.UnreadEmails]'."

i can understand the exception but couldnt fine solution pls help how can i do it
Posted
Comments
Simon_Whale 25-Aug-10 10:16am    
it would help more if you could tell where the error occurs? does this happen in production? or delvelopment environment?
mslliviu 26-Aug-10 3:50am    
what type of object does dgvUnreadMails.DataSource contain at runtime when you run Filter function? How did you bind the grid?

1 solution

ok lets attempt to resolve it..

The problem is with the explicit cast of the linq query result to a list before assigning it to filtered list because by default the results of the linq query is returned in a generic collection that is why it allows you to use var type..so casting it to a string list would be impossible ( as in the case you previously wanted to do)

..so what you should do is to first create a generic list List<string> for all the previous lists you created and then secondly, in the foreach loop,
return the query to a variable with the IEnumerable<string> or the var type. (I would personally recommend the IEnumerable<string> type to avoid type and value ambiguity).

and then since its already an enumerable generic collection type, you can easily copy all its contents directly into the list type you initially wanted to do

so I think you should modify that section of your code to look like this..

foreach(string item in words)
{
   IEnumerable<string> q = from ci in list where ci.BodyContent.ToLower().Contains(item) select ci;

//now use a nested foreach loop to push the items in q into a generic List<string> instead..It should work
   
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900