小编san*_*ngh的帖子

处理LINQ查询中的NULL参数

假设你的LINQ查询where子句中有参数,你如何处理?

这是一个例子:

var peoples= from i in individuals
  where (string.IsNullOrEmpty(lastName) i.LastName.Equals(lastName))
  select i;
Run Code Online (Sandbox Code Playgroud)

c# linq

3
推荐指数
3
解决办法
1846
查看次数

为什么我会收到InvalidCastException?

我在c#中有以下代码片段

List<int> list = new List<int>() { 1, 23, 5, 3, 423, 3 };
            var query = list.Cast<double>().Select(d => d);
            try
            {
                foreach (var item in query)
                {
                    Console.WriteLine(item);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }
Run Code Online (Sandbox Code Playgroud)

它编译得很完美,但是当我执行它时,我得到了异常.

c# linq

3
推荐指数
2
解决办法
2377
查看次数

在c#中查询excel表

我想使用以下代码在c#中读取Excel文件

string excelFileName = "Book2.xls";
string excelConnectString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Book2.xls;Extended Properties=""Excel 8.0;HDR=YES;""";
//string excelConnectString = @"Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + excelFileName + ";" + "Extended Properties = Excel 8.0; HDR=Yes;IMEX=1";

OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand objCmd = new OleDbCommand("Select * From [Sheet1$]", objConn);

OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
objDatAdap.SelectCommand = objCmd;
DataSet ds = new DataSet();
objDatAdap.Fill(ds);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

一切都工作正常.现在我的要求是阅读下面的excel文件

SELECT A,B,D From [Sheet1];
Run Code Online (Sandbox Code Playgroud)

c# excel ms-office

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

多个线程在C#中调用相同的方法

我有以下C#代码片段,我在其中模拟了我的问题.在这个程序中,我有一个调用ReadRooms方法的Service函数.现在我在不同的线程上调用服务方法.我期待ServiceCall和ReadRooms方法都会被平分,但我得到的结果不正确.

在此输入图像描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        public static void ReadRooms(int i)
        {
            Console.WriteLine("Reading Room::" + i);
            Thread.Sleep(2000);
        }
        public static void CallService(int i)
        {
            Console.WriteLine("ServiceCall::" + i);
            ReadRooms(i);

        }
        static void Main(string[] args)
        {
            Thread[] ts = new Thread[4];
            for (int i = 0; i < 4; i++)
            {
                ts[i] = new Thread(() =>
                    {
                        int temp = i;
                        CallService(temp);


                    });
                ts[i].Start();
            }
            for (int i = …
Run Code Online (Sandbox Code Playgroud)

c# static multithreading

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

基类构造函数的虚函数

我在下面的代码片段中总结了我的问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace St
{
    public  class Animal
    {
        public Animal()
        {
            Speak();
        }
      public virtual void Speak()
      {
          Console.WriteLine("Animal speak");
      }
    }
    public  class Dog:Animal
    {
        private StringBuilder sb = null;

        public Dog()
        {
                sb=new StringBuilder();
        }
        public override void Speak()
        {
            Console.WriteLine("bow...{0}",sb.Append("bow"));
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Dog d=new Dog();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译它时没有错误但是当我运行它时我得到对象引用错误.

c#

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

LINQ:检查字符串是否是回文?

我想检查字符串是否是回文使用Linq.

更新:

我想要使​​用反向功能.

c# linq

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

哪一个更适合获得准确的性能反应:DateTime还是秒表?

我知道DateTime和Stopwatch类,但我有点困惑.我只是想知道哪一个更好,为什么?

c#

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

WPF UI 未更新

我已经使用 MVVM 模型和代码开发了一个简单的 wpf 应用程序,如下所示。我调试代码并发现集合正在更新但 UI 没有更新,即使我在列表框中没有看到任何记录。

编辑

在此处输入图片说明

<Window x:Class="Model.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Dispatcher Demo" Height="350" Width="525">


    <DockPanel>
        <Grid DockPanel.Dock="Bottom">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*" Name="col0" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBox Name="textBlock1" Text="{Binding ShowPath}" 
                 VerticalAlignment="Center" HorizontalAlignment="Left" 
                  Margin="30" Grid.Column="0" />
            <Button Name="FindButton" Content="Select Path" 
              Width="100" Margin="20" Click="FindButton_Click" Grid.Column="1" />
        </Grid>
        <ListBox Name="listBox1"  ItemsSource="{Binding Path=Files}"/>


    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Threading;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace Model
{
    public class DirectorySearchModel : INotifyPropertyChanged
    { …
Run Code Online (Sandbox Code Playgroud)

c# wpf mvvm

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

Mongoose Cast to ObjectId 的值失败

我在猫鼬中有以下模式

var mongoose = require("mongoose");

var Schema = mongoose.Schema;
var UserSchema = new mongoose.Schema({
  email: { type: String, unique: true },

});
var User = mongoose.model('User', UserSchema);
var ReviewSchema = new Schema({
  schoolId: String,
  firstname: String,
  image_url: String,
  lastname: { type: String },
  email: String,
  rating: Number,
  review: String,
  time: { type: Date, default: Date.now },
  updated_at: Date,
  voters: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }]

});


var Review = mongoose.model('Review', ReviewSchema);


module.exports = Review;
Run Code Online (Sandbox Code Playgroud)

和 nodejs 中的以下代码

app.post("/like", function (req, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

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

值类型和引用类型

我在下面的代码片段中总结了我的问题.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication13
{
    public class Student
    {
        public int Marks { get; set; }
        public Student(int marks)
        {
            this.Marks = marks;

        }
        public void AssignMarks(Student st)
        {
            st = null;
        }
        public void AssignMarks(ref Student st)
        {
            st = null;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Student st = new Student(50);
            st.AssignMarks(st);
            Console.WriteLine(st.Marks);
            Student st1 = new Student(50);
            st.AssignMarks(ref st1); // NullReferenceException
            Console.WriteLine(st1.Marks);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我在标有**的行上得到NullReferenceException异常

c#

1
推荐指数
1
解决办法
259
查看次数

标签 统计

c# ×9

linq ×3

excel ×1

mongodb ×1

mongoose ×1

ms-office ×1

multithreading ×1

mvvm ×1

node.js ×1

static ×1

wpf ×1