Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a problem while filtering my data.

I have two tables one is the main table where all the data is stored. There is another table where the keywords are stored which is required to filter data from Main table.

The problem i am facing is that i am not able to put up the exact query which is required as the keywords which arrive in the Main Table Data are required to be filtered out.

For example if there are 10 words in Keyword table then Main table data have to be searched thorough all these keywords so that it can be filtered out correctly.


I hope i have made the question clear.

Also i am using LINQ TO SQL solution in it is most welcome
Posted

see below if this is what you are expecting:
DataTable table = new DataTable();
   table.Columns.Add("ID", typeof(int));
   table.Columns.Add("Drug", typeof(string));
   table.Columns.Add("Patient", typeof(string));
   table.Columns.Add("Date", typeof(DateTime));


   table.Rows.Add(25, "Indocin", "David", DateTime.Now);
   table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
   table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
   table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
   table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);


   DataTable lookuptable = new DataTable();
   lookuptable.Columns.Add("lookupID", typeof(int));
   lookuptable.Rows.Add(25);
   lookuptable.Rows.Add(50);
   lookuptable.Rows.Add(10);
   lookuptable.Rows.Add(21);

   var col = from dr in table.AsEnumerable()
             join
             dr1 in lookuptable.AsEnumerable() on dr.Field<int>("ID") equals dr1.Field<int>("lookupID")
             select dr;

           foreach (var item in col)
   {
       Console.WriteLine(item.Field<string>("Patient"));
   }

           Console.ReadLine();
 
Share this answer
 
Comments
Khawaja Jibran 16-Dec-13 2:19am    
its not that what I am looking. Let me give you an example what i exactly want :)

There is a field in Main table "Title" and another field "Description".
There is another table Keyword Table with fields "Id" and "Word".

Main Table data

This is title This is Description
This Row is Selected This one does not have the keyword so this row must be selected.

Keywords Table
1 Title
2 Description

So i need a query which will get these keywords and search which title and description does not contain it and select that row(s)
hi
Try like this..

C#
using System.Collections.Generic;
using System.Linq;

namespace test
{

    class Program
    {
        static void Main(string[] args)
        {

            List<maintable> lstMainTable = new List<maintable>();
            lstMainTable.Add(new MainTable() { Name = "apple", Others = "aaa" });
            lstMainTable.Add(new MainTable() { Name = "ball", Others = "bbb" });
            lstMainTable.Add(new MainTable() { Name = "cat", Others = "ccc" });
            lstMainTable.Add(new MainTable() { Name = "dog", Others = "ddd" });
            lstMainTable.Add(new MainTable() { Name = "elepant", Others = "eeee" });
            lstMainTable.Add(new MainTable() { Name = "goat", Others = "ggg" });
            lstMainTable.Add(new MainTable() { Name = "kite", Others = "kkk" }); 
            lstMainTable.Add(new MainTable() { Name = "lemon", Others = "llll" });


            List<keywords> lstKeyWords = new List<keywords>();
            lstKeyWords.Add(new KeyWords() { Key = "apple", OtherColumn = "aa" });
            lstKeyWords.Add(new KeyWords() { Key = "cat", OtherColumn = "aa" });
            lstKeyWords.Add(new KeyWords() { Key = "goat", OtherColumn = "aa" });


            var lstOutput = lstMainTable.Where(k => lstKeyWords.Select(k0=>k0.Key).Contains(k.Name)).ToList();
            
        }
    }

    public class MainTable
    {
        public string Name { get; set; }
        public string Others { get; set; }
    }

    public class KeyWords
    {
        public string Key { get; set; }
        public string OtherColumn { get; set; }
    }
}
 
Share this answer
 
v2

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