LINQ和Except [英] LINQ and Except

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

问题描述

我正在使用Except选择一个文件(今天的)中的所有记录(称为增量记录),这些记录不在另一个文件中(昨天)。这些文件每天总是增加零或更多记录,并且记录永远不会被删除,但有些可能会更改,有些可能会增加

I'm using Except to select all records (call them delta records) in one file (today's) that were not in another file (yesterday's). These files always grow by zero or more records each day and records are never removed but some may change and some may get added.

LINQ 工作正常但是我还需要报告我们从增量记录处理的每条记录的行号(在今天的文件中)。

The LINQ Except works fine however I also need to report the line number (in today's file) of each record we process from the delta records.

我这样做只需要调用 todays.IndexOf (记录)哪个有效,但在我正在编写的应用程序中有60%的CPU成本。这些文件包含超过75,000条记录(但增量记录可能只有2,000条左右)。

I do this simply by calling todays.IndexOf(record) which works but has a 60% CPU cost in the app I'm writing. The files contains upwards of 75,000 records (yet the delta records may be just 2,000 or so).

我尝试创建一次这些记录号的列表(同时我做了
之外)然后获取记录号只是在更小的列表(2000左右)上查找
IndexOf 的问题。但CPU上升是因为我们正在做 - 总体而言 - 总共工作量更多。

I tried creating a list of these record numbers once (at the same time I do the Except) and then getting the record number is just a matter of looking up the IndexOf on the much smaller list (2000 or so). But the CPU went up because we're doing - overall - more work in total.

所以我想知道有什么喜欢 除了这不仅可以给出A中不在B中的记录,还可以给出A中记录的索引。

So I wanted to know is there anything like Except that can give me not just the records from A that are not in B but also the index of the record within A.

谢谢

推荐答案

嗨船长,

感谢您在此发帖。

对于你的问题,你想知道除了解决你的性能问题之外还有什么。

For your question, you wanted to know is there anything like Except to solve your performance problems.

我建议你可以将数据存储到文件内容中然后使用SQL语句。

I would suggest that you could store the data in the file content into the database and then use the SQL statement.

这是一个供你参考的简单示例。

Here is a simple example for your reference.

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Data.SqlClient;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace test4

{

    class Program

    {

        static void Main(string[] args)

        {

            string str= @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = test; Integrated Security = True; Connect Timeout = 30; Encrypt = False; TrustServerCertificate = False; ApplicationIntent = ReadWrite; MultiSubnetFailover = False";

            DateTime dateTime = DateTime.Now;

            using (SqlConnection conn = new SqlConnection(str))

            {

                conn.Open();

                SqlCommand cmd = conn.CreateCommand();

                cmd.CommandText = "select *from today except select *from yesterday";

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())

                {

                    string txt = reader.GetString(reader.GetOrdinal("txt"));

                    int index = reader.GetInt32(reader.GetOrdinal("id"));

                    Console.WriteLine(txt+"  " + index);

                }

            }

            Console.WriteLine(DateTime.Now-dateTime);

            Console.ReadKey();

        }

        }

   

}

祝你好运,

张龙






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

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