假设你的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#中有以下代码片段
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#中读取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#代码片段,我在其中模拟了我的问题.在这个程序中,我有一个调用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) 我在下面的代码片段中总结了我的问题
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)
当我编译它时没有错误但是当我运行它时我得到对象引用错误.
我已经使用 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) 我在猫鼬中有以下模式
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) 我在下面的代码片段中总结了我的问题.
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异常