C#Where<>条款帮助!!! [英] C# Where<> Clause Help!!!

查看:47
本文介绍了C#Where<>条款帮助!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助为函数编写where子句,有人可以帮忙吗?



该函数名为GetFilesByCreationDate。它的目标是按创建日期(System.DateTime)搜索文件。到目前为止,我已经编写了大部分代码。

它现在需要检查2件事,但我不知道如何为这个解决方案做任何事情并需要帮助。



1.)查找找到的每个文件的创建日期时间。

2.)将File Found Creation DateTime与方法参数modify进行比较。 />


根据我的猜测,Where子句可以工作,但我不知道如何实现它。





  public   static 列表<串GT; GetFilesByCreationDate( String  searchDirectory, Boolean  searchSubDirectories,DateTime modify)
{
List<串GT; result = new List< String>();
if (IsDirectory(searchDirectory))
result = Directory.GetFiles(searchDirectory, *。*,(SearchOption)(System.Convert.ToInt32(searchSubDirectories)))。ToList< String>()。Where<> ;;
返回结果;
}

解决方案

我想尝试帮助您从一开始就提出一些建议。请记住,我来这里是为了帮助您学习编程。



I.检查后确保您指定的目录存在



a。使用 new DirectoryInfo(searchDirectory)从有效目录路径获取DirectoryInfo对象



II。然后在该DirectoryInfo实例上使用EnumerateFiles,使用您现在使用的'修改参数。



a。这些目录/文件搜索方法需要Type'SearchOption的标志来确定搜索是否是递归的;你需要使用你的'searchSubDirectories bool来选择其中一个选项,如下所示:

 SearchOption currentSearchOption =(searchSubDirectories)
? SearchOption.AllDirectories
:SearchOption.TopDirectoryOnly);

a。 EnumerateFiles返回 IEnumerable< FileInfo> ,并且其中的每个FileInfo实例都可以轻松访问FileTime参数,例如'CreationTime等.EnumerateFiles也更快,更高效,在许多情况比使用'GetFiles [ ^ ]。



III。这里更有趣的挑战是你如何编写一个可以使用不同比较函数的方法。以下是如何执行此操作的简单示例:

  public   bool  DateCompare( DateTime dt1,DateTime dt2,Func< DateTime,DateTime,bool> compareFunc)
{
return compareFunc(dt1,dt2);
}

// 上述方法的样本测试
// 在某些方法或EventHandler中执行
bool test1 = DateCompare(
DateTime.Now,
new DateTime( 2000 1 1 1 1 1 ),
// date1大于date2
(dt1,dt2)= > dt1 > dt2);

bool test2 = DateCompare(
DateTime.Now,
DateTime.Now,
// date1与date2相同
(dt1,dt2)= > { return dt1 == dt2;});

bool test3 = DateCompare(
DateTime.Now,
new DateTime( 2020 1 1 1 1 1 ),
// date1小于date2
(dt1,dt2) = > {返回 dt1 < dt2;});

在此处显示的方法中使用'Func参数允许您传入可执行的代码块,并在需要时执行它。 'Func实际上是使用'代表'的简便方式;它具有'委托类型的所有功能。



您的挑战是获取此处显示的信息,然后使用此处显示的技术构建您的Linq查询。



提示:在你的方法中添加一个新的'Func参数。



快乐学习!


Need help writing a where clause for a function, can anyone please help?

The function is called "GetFilesByCreationDate". It's goal is to search for files by creation date (System.DateTime). I have most of the code written so far.
It needs to now check for 2 things, but I'm not sure of how to do either for this solution and need help.

1.) Find Creation DateTime of each file it finds.
2.) Compare the File Found Creation DateTime to the method Parameter "modify".

From what I'm guessing, a Where clause would work but I have no clue how to implement it.


public static List<String> GetFilesByCreationDate(String searchDirectory, Boolean searchSubDirectories, DateTime modify)
        {
            List<String> result = new List<String>();
            if (IsDirectory(searchDirectory))
                result = Directory.GetFiles(searchDirectory, "*.*", (SearchOption)(System.Convert.ToInt32(searchSubDirectories))).ToList<String>().Where<>;
            return result;
        }

解决方案

I want to try and help get you started here with a few suggestions. Keep in mind that I'm here to help you learn programming.

I. after you check to make sure the Directory you specify exists

a. use new DirectoryInfo(searchDirectory) to get a DirectoryInfo object from the valid directory path

II. then use EnumerateFiles on that DirectoryInfo instance, using the 'modify parameters you use now.

a. these directory/file search methods require a flag of Type 'SearchOption to determine whether the search is recursive or not; you'll need to use your 'searchSubDirectories bool to select one of the options, like this:

SearchOption currentSearchOption = (searchSubDirectories) 
    ? SearchOption.AllDirectories 
    : SearchOption.TopDirectoryOnly);

a. EnumerateFiles returns an IEnumerable<FileInfo>, and, each FileInfo instance in it provides easy access to File parameters like 'CreationTime, etc. EnumerateFiles is also faster, more efficient, in many circumstances than using 'GetFiles [^].

III. the more interesting challenge here is how you can write one method that can use different comparison functions. Here's a simple example of how to do that:

public bool DateCompare(DateTime dt1, DateTime dt2, Func<DateTime, DateTime, bool> compareFunc)
{
    return compareFunc(dt1, dt2);
}

// sample tests of the above method
// execute in some method or EventHandler
bool test1 = DateCompare(
    DateTime.Now,
    new DateTime(2000, 1, 1, 1, 1, 1),
    // date1 greater than date2
    (dt1, dt2) => dt1 > dt2);

bool test2 = DateCompare(
    DateTime.Now,
    DateTime.Now,
    // date1 same as date2
    (dt1, dt2) => { return dt1 == dt2; });

bool test3 = DateCompare(
    DateTime.Now,
    new DateTime(2020, 1, 1, 1, 1, 1),
    // date1 less than date2
    (dt1, dt2) => { return dt1 < dt2; });

The use of a 'Func parameter in the method shown here allows you to pass in an executable block of code, and execute it when you want to. 'Func is really a short-hand way of using a 'Delegate; it has all the functionality of the 'Delegate Type.

Your challenge is to take the information shown here, and then construct your Linq query, using the techniques shown here.

Hint: add a new 'Func parameter to your method.

Happy Learning !


这篇关于C#Where&lt;&gt;条款帮助!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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