通过LINQ to SQL过滤数据 [英] Filtering data by LINQ to SQL

查看:55
本文介绍了通过LINQ to SQL过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我在过滤数据时遇到问题。



我有两个表,一个是存储所有数据的主表。还有另一个表格,其中存储了从主表格中过滤数据所需的关键字。



我面临的问题是我无法忍受需要过滤掉主表数据中的关键字所需的确切查询。



例如,如果Keyword表中有10个单词,那么Main必须通过所有这些关键字搜索表数据,以便可以正确过滤掉。





我希望我提出了这个问题明确。



另外我使用的LINQ TO SQL解决方案非常受欢迎

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

推荐答案

见下文如果这是你所期望的:

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();


hi


试试这样..



hi
Try like this..

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; }
    }
}


这篇关于通过LINQ to SQL过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆