سؤال

ولدي قدر لا بأس به من الأسئلة وأول واحد هو كيف يمكن أن أفعل استعلام LINQ بسيطة لتتناسب كلمة في الملف؟ أنا لا أحاول أن أكون غبيا ولكن أنا لم يفهم الوثائق التي وجدت لLINQ بشكل صحيح.

هل كانت مفيدة؟

المحلول

وإنشاء تطبيق WindowsForms جديد واستخدام التعليمات البرمجية التالية.

والحاجة you''ll لإضافة عنصر تحكم التسمية التسمية، النص، وزر

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;

namespace LinqTests
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public String[]
            Content;
        public String
        Value;

        private void button1_Click(object sender, EventArgs e)
        {
            Value = textBox1.Text;

            OpenFileDialog ofile = new OpenFileDialog();
            ofile.Title = "Open File";
            ofile.Filter = "All Files (*.*)|*.*";

            if (ofile.ShowDialog() == DialogResult.OK)
            {
                Content =
                       File.ReadAllLines(ofile.FileName);

                IEnumerable<String> Query =
                    from instance in Content
                    where instance.Trim() == Value.Trim()
                    orderby instance
                    select instance;

                foreach (String Item in Query)
                    label1.Text +=
                        Item + Environment.NewLine;
            }
            else Application.DoEvents();

            ofile.Dispose();
        }
    }
}

وآمل أن يساعد هذا

نصائح أخرى

وماذا عن شيء من هذا القبيل ما يلي؟

string yourFileContents = File.ReadAllText("c:/file.txt");
string foundWordOrNull =  Regex.Split(yourFileContents, @"\w").FirstOrDefault(s => s == "someword");

و(الذي قال من أي وقت مضى أن C # لا يمكن أن يكون موجزة؟)

والشركة المصرية للاتصالات كود يعمل من خلال قراءة الملف الخاص بك، وتقسيم ذلك إلى كلمات ثم تعود الكلمة الأولى وجدت أن يسمى someword.

تحرير: من تعليق ما سبق كان يعتبر "لا LINQ". على الرغم من أنني لا نتفق (انظر تعليق)، وأنا لا أعتقد أن هناك ما يبرر على سبيل المثال أكثر LINQified النهج نفسه هنا؛ -)

string yourFileContents = File.ReadAllText("c:/file.txt");
var foundWords =  from word in Regex.Split(yourFileContents, @"\w")
                  where word == "someword"
                  select word;

if(foundWords.Count() > 0)
    // do something with the words found

وهنا مثال من MSDN التي تهم تكرارات كلمة في سلسلة (<لأ href = "http://msdn.microsoft.com/en-us/library/bb546166.aspx" يختلط = "noreferrer نوفولو" > http://msdn.microsoft.com/en-us/library/bb546166.aspx ).

string text = ...;

string searchTerm = "data";

//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },
    StringSplitOptions.RemoveEmptyEntries);

// Create and execute the query. It executes immediately 
// because a singleton value is produced.
// Use ToLowerInvariant to match "data" and "Data" 
var matchQuery = from word in source
         where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
         select word;

// Count the matches.
int wordCount = matchQuery.Count();
Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.",
    wordCount, searchTerm);

وهنا هو واحد أكثر LINQ البرنامج التعليمي على قراءة البيانات من ملف نصي <لأ href = "http://www.onedotnetway.com/tutorial-reading-a-text-file-using-linq/" يختلط = "نوفولو noreferrer "> http://www.onedotnetway.com/tutorial-reading-a-text-file-using-linq/ .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top