frmcollection.cs

来自「移动设备的 LINQ 编程介绍 .NET Compact Framework 版」· CS 代码 · 共 61 行

CS
61
字号
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Demo1___LinqToObjects
{
	public partial class FrmCollection : Form
	{
		string appPath;

		public FrmCollection()
		{
			InitializeComponent();

			appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
		}

		private void menuItem1_Click(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			// Create the IEnumerable data sources.
			string[] names1 = Utility.ReadAllLines(appPath + @"\names1.txt");
			string[] names2 = Utility.ReadAllLines(appPath + @"\names2.txt");

			// Create the query. Note that method syntax must be used here.
			IEnumerable<string> differenceQuery =
			  names1.Except(names2);

			StringBuilder sb = new StringBuilder();
			sb.AppendLine("The following lines are in names1.txt but not names2.txt").AppendLine();
			foreach (string s in differenceQuery)
			{
				sb.AppendLine(s);
			}
			sb.AppendLine();

			// Find the names that occur in both files (based on
			// default string comparer).
			IEnumerable<string> intersectQuery =
				names1.Intersect(names2);

			sb.AppendLine("The following lines are both in names1.txt and names2.txt").AppendLine();
			foreach (string s in intersectQuery)
			{
				sb.AppendLine(s);
			}

			textBox1.AppendLine(sb.ToString());

			Cursor.Current = Cursors.Default;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?