小编cra*_*opy的帖子

将字符串转换为DateTime

你如何将字符串2009-05-08 14:40:52,531转换为DateTime

c# datetime

553
推荐指数
12
解决办法
126万
查看次数

LINQ to Entities无法识别方法'System.String Format(System.String,System.Object,System.Object)'

我有这个linq查询:

private void GetReceivedInvoiceTasks(User user, List<Task> tasks)
{
    var areaIds = user.Areas.Select(x => x.AreaId).ToArray();

    var taskList = from i in _db.Invoices
                   join a in _db.Areas on i.AreaId equals a.AreaId
                   where i.Status == InvoiceStatuses.Received && areaIds.Contains(a.AreaId)
                   select new Task {
                       LinkText = string.Format(Invoice {0} has been received from {1}, i.InvoiceNumber, i.Organisation.Name),
                       Link = Views.Edit
                   };
}
Run Code Online (Sandbox Code Playgroud)

它有问题.我正在尝试创建任务.对于每个新任务,当我将链接文本设置为像"Hello"这样的常量字符串时,它很好.但是上面我试图使用发票的属性来构建属性linktext.

我收到此错误:

base {System.SystemException} = {"LINQ to Entities无法识别方法'System.String Format(System.String,System.Object,System.Object)'方法,并且此方法无法转换为商店表达式." }

谁知道为什么?有人知道这样做的另一种方法是让它起作用吗?

linq linq-to-entities entity-framework

80
推荐指数
3
解决办法
9万
查看次数

控制台应用程序的进度条

我正在写一个简单的c#控制台应用程序,它将文件上传到sftp服务器.但是,文件量很大.我想显示已上传文件的百分比,或者只显示已上传文件的数量,以及要上传的文件总数.

首先,我获取所有文件和文件总数.

string[] filePath = Directory.GetFiles(path, "*");
totalCount = filePath.Length;
Run Code Online (Sandbox Code Playgroud)

然后我遍历文件并在foreach循环中逐个上传它们.

foreach(string file in filePath)
{
    string FileName = Path.GetFileName(file);
    //copy the files
    oSftp.Put(LocalDirectory + "/" + FileName, _ftpDirectory + "/" + FileName);
    //Console.WriteLine("Uploading file..." + FileName);
    drawTextProgressBar(0, totalCount);
}
Run Code Online (Sandbox Code Playgroud)

在foreach循环中,我有一个进度条,我遇到了问题.它无法正常显示.

private static void drawTextProgressBar(int progress, int total)
{
    //draw empty progress bar
    Console.CursorLeft = 0;
    Console.Write("["); //start
    Console.CursorLeft = 32;
    Console.Write("]"); //end
    Console.CursorLeft = 1;
    float onechunk = 30.0f / total;

    //draw filled part
    int position = 1;
    for …
Run Code Online (Sandbox Code Playgroud)

c# console progress

68
推荐指数
8
解决办法
7万
查看次数

使图像适合PictureBox

using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (SqlCommand SqlCommand = new SqlCommand("Select Photo from Employee where EmpID LIKE '%' + @EmpID + '%' ", myDatabaseConnection))
    {
        SqlCommand.Parameters.AddWithValue("@EmpID", textBox1.Text);
        DataSet DS = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(SqlCommand);
        adapter.Fill(DS, "Images");

        var imagesTable = DS.Tables["Images"];
        var imagesRows = imagesTable.Rows;
        var count = imagesRows.Count;

        if (count <= 0)
            return;
        var imageColumnValue =
            imagesRows[count - 1]["Image"];
        if (imageColumnValue == DBNull.Value)
            return;

        var data = (Byte[])imageColumnValue;
        using (var stream = new MemoryStream(data)) …
Run Code Online (Sandbox Code Playgroud)

c# image picturebox winforms

48
推荐指数
6
解决办法
13万
查看次数

有没有比这更快的方法来查找目录和所有子目录中的所有文件?

我正在编写一个程序,需要在目录及其所有子目录中搜索具有特定扩展名的文件.这将在本地和网络驱动器上使用,因此性能有点问题.

这是我现在使用的递归方法:

private void GetFileList(string fileSearchPattern, string rootFolderPath, List<FileInfo> files)
{
    DirectoryInfo di = new DirectoryInfo(rootFolderPath);

    FileInfo[] fiArr = di.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly);
    files.AddRange(fiArr);

    DirectoryInfo[] diArr = di.GetDirectories();

    foreach (DirectoryInfo info in diArr)
    {
        GetFileList(fileSearchPattern, info.FullName, files);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以将SearchOption设置为AllDirectories而不使用递归方法,但将来我会插入一些代码来通知用户当前正在扫描的文件夹.

现在我正在创建一个FileInfo对象列表,我真正关心的是文件的路径.我将有一个现有的文件列表,我想将其与新的文件列表进行比较,以查看添加或删除了哪些文件.有没有更快的方法来生成这个文件路径列表?有什么办法可以优化这个文件搜索来查询共享网络驱动器上的文件吗?


更新1

我尝试创建一个非递归方法,通过首先查找所有子目录,然后迭代扫描每个目录中的文件来执行相同的操作.这是方法:

public static List<FileInfo> GetFileList(string fileSearchPattern, string rootFolderPath)
{
    DirectoryInfo rootDir = new DirectoryInfo(rootFolderPath);

    List<DirectoryInfo> dirList = new List<DirectoryInfo>(rootDir.GetDirectories("*", SearchOption.AllDirectories));
    dirList.Add(rootDir);

    List<FileInfo> fileList = new List<FileInfo>();

    foreach (DirectoryInfo dir in dirList)
    {
        fileList.AddRange(dir.GetFiles(fileSearchPattern, SearchOption.TopDirectoryOnly));
    }

    return fileList;
}
Run Code Online (Sandbox Code Playgroud)

更新2

好吧,所以我在本地和远程文件夹上运行了一些测试,这两个文件夹都有很多文件(~1200).以下是我运行测试的方法.结果如下. …

.net c# directory file-io

36
推荐指数
5
解决办法
5万
查看次数

获得随机颜色

你知道任何生成随机颜色的方法(不是随机颜色名称!)?

我已经有了一个,但这个没有正确地做到:

这只返回绿色:

Random r = new Random();
BackColor = Color.FromArgb(r.Next(0, 256), r.Next(0, 256), 0);
Run Code Online (Sandbox Code Playgroud)

这只返回红色:

Random r = new Random();
BackColor = Color.FromArgb(r.Next(0, 256), 0, 0);
Run Code Online (Sandbox Code Playgroud)

这只返回蓝色:

Random r = new Random();
BackColor = Color.FromArgb(0, 0, r.Next(0, 256));
Run Code Online (Sandbox Code Playgroud)

我希望我的代码返回一个随机颜色,不仅每次都返回绿色/红色/蓝色,如上所述.

怎么解决这个?

任何建议都会得到快乐的批准!

c# random colors backcolor winforms

15
推荐指数
2
解决办法
5万
查看次数

是否可以使用Epplus在Excel中复制行(包含数据,合并,样式)?

问题是我需要使用整个集合的单个模板多次从集合中将数据插入Excel.

using (var pckg = new ExcelPackage(new FileInfo(association.TemplatePath)))
{
    var workSheet = pckg.Workbook.Worksheets[1];
    var dataTable = WorksheetToDataTable(workSheet);
    /*Some stuff*/
    FindAndReplaceValue(workSheet, dictionary, row);
}

private DataTable WorksheetToDataTable(ExcelWorksheet oSheet)
{
    int totalRows = oSheet.Dimension.End.Row;
    int totalCols = oSheet.Dimension.End.Column;
    DataTable dt = new DataTable(oSheet.Name);
    DataRow dr = null;
    for (int i = 1; i <= totalRows; i++)
    {
        if (i > 1) dr = dt.Rows.Add();
        for (int j = 1; j <= totalCols; j++)
        {
            if (i == 1)
                dt.Columns.Add((oSheet.Cells[i, j].Value ?? "").ToString()); …
Run Code Online (Sandbox Code Playgroud)

.net c# excel epplus

15
推荐指数
1
解决办法
2万
查看次数

如何在ViewCell中更改高度

我正在尝试在listview上更改ViewCell,但下面的代码对我不起作用:

<DataTemplate>
    <ViewCell Height="100">
        <StackLayout Orientation="Horizontal">
            <Image Source="{Binding Seller.Thumbnail}}" Aspect="AspectFit" />
            <StackLayout Orientation="Vertical" >
                <Label Text="{Binding CouponName}" FontAttributes="Bold" FontSize="12" />
                <Label Text="{Binding EndOffer}" FontSize="11" />
            </StackLayout>
        </StackLayout>
    </ViewCell>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

c# xaml xamarin-forms

13
推荐指数
4
解决办法
2万
查看次数

Atom/Sublime喜欢Jupyter中的Multiple选择

如何通过键盘快捷键在Jupyter笔记本中选择匹配的关键字?例如,在Atom/Sublime编辑器中,当光标位于'var'时,我可以点击cmd + Dmac(或Ctrl + d在Windows上),每次我这样做时,下一个'var'将被突出显示.然后我可以输入新的变量名称,并将"var"替换为我输入的内容.

var = "hello"
print(var)
print(var)
Run Code Online (Sandbox Code Playgroud)

在Jupyter笔记本中是否有相同的东西?

keymapping jupyter-notebook

13
推荐指数
2
解决办法
2222
查看次数

TypeScript:__ dirname值?

我的文件夹结构是这样的:

  • 应用
    • tsFolder
      • XX.ts

XX.ts文件中的代码:

var __dirname: string;
console.log("__dirname: " + __dirname); // the output is undefined.
Run Code Online (Sandbox Code Playgroud)

我试过了:通过注释掉代码var __dirname: string;,值__dirname是:...../app指向当前文件夹的insead就好了...../app/tsFolder.

有人能解释一下吗?

typescript

9
推荐指数
2
解决办法
1万
查看次数