本文介绍了从C#中的数据集中创建具有很少记录的数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,该数据集包含50多个行,每个行都有一个唯一的ID,例如Invoivce#121,Invoivce#122等的InvoiceNumbers.
现在,我有了一个包含很少发票编号的字符串列表{Invoivce#121,Invoivce#122,Invoivce#124,Invoivce#125}.现在,我需要从数据集中检索清单中包含的发票编号的所有记录.帮帮我,..提前谢谢..

我希望将选定的记录保存到另一个数据集或datatable.pls帮助e在c#中为我的ASP.Net应用程序执行此操作...

i have a dataset which has over 50 rows each having a unique id as InvoiceNumbers like Invoivce#121,Invoivce#122 etc.
Now i have a string list which has few invoice numbers{Invoivce#121,Invoivce#122,Invoivce#124,Invoivce#125}.Now i need to retrieve all the records from the dataset with invoice number which i have in my list.Help me how to do this,..thanks in advance..

i want the selected records to be saved in another dataset or in a datatable.pls help e to do this in c# for my ASP.Net application...

推荐答案


List<invoice> lstInvoice = new List<invoice>();
foreach(string str in strLst)
{
  for(int i=0;i<ds.table[0].rows.count;i++)>
  {
     if(str.Equals(ds.Table[0].Row[i]["InvoiceNumber"].ToString()))
     {
        Invoice inv = new Invoice();
        inv.InvoiceNumber = ds.Table[0].Row[i]["InvoiceNumber"].ToString(); //typecast same as inv.InvoiceNumber 
        lstInvoice.Add(inv);
     }
  }
}

After this you can save lstInvoice into the database or wherever you want.

Hope this will help you.

</invoice></invoice>


1   Amit
2   Test1
3   Test2
4   Test3
5   test4


您的ID是:


And your ID is:

int[] id = new int []{ 1, 3, 5 };


所以您的查询应该是这样的:


So your query should be something like this:

var query = (from s in dt.AsEnumerable()
                        join p in id.AsEnumerable() on s.Field<int>("ID") 
                        equals Convert.ToInt32(p)
                        select s);
DataTable FilteredData = query.Count() > 0 ? query.CopyToDataTable() : null;</int>


因此,此查询将为您提供结果数据表,并且输出将如下所示:


So, this query will give you the resultant datatable and the output will be like this:

1   Amit
3   Test2
5   test4




希望对您有帮助.
--- Amit




Hope it helped you.
---Amit


这篇关于从C#中的数据集中创建具有很少记录的数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 22:58