我试图将我的所有数据写入文本文件,除非我DateTime输入文件名,否则它正在工作.
当前代码如下所示:
string time = DateTime.Now.ToString("d");
string name = "MyName";
File.WriteAllText(time+name+"test.txt","HelloWorld");
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
mscorlib.dll中发生未处理的"System.IO.DirectoryNotFoundException"类型异常
但据我所知,该File.WriteAllText()方法应该创建一个新文件或覆盖已存在的文件.
有什么建议?
我试图将表达式作为参数传递给函数,如:
public string MyFunction(bool expression) //what should I set here as parameter type?
{
return expression ? "True" : "False";
}
public void InvokeMyFunction()
{
var myString = MyFunction(10 > 1);
}
Run Code Online (Sandbox Code Playgroud) 尝试动态呈现按钮并在每行中放置 3 个按钮,但它们都出现在屏幕上的单独一行中?
<div class="row">
<div class="container sports-container col-md-12">
@foreach (var sport in Model.Aggregator.SportsRepository.List().Where(x => x.ParentSportId == null))
{
<div class="col-md-4">
<a class="btn btn-outline-info">@sport.Description</a>
</div>
}
</div>
Run Code Online (Sandbox Code Playgroud)
<style>
.sports-container {
background-color: whitesmoke;
margin-top: 20px;
}
</style>
Run Code Online (Sandbox Code Playgroud) 我想使用@sign作为一个字符串,但它被检测为剃刀语法的起点,有没有办法解决它?
我想获得Patient的Firstname从数据库中,但什么我得到当我运行这段代码仅仅是一个LINQ表达式字符串,而不是.
public static string GetPatientName(int bedNumber)
{
var name = "";
using (var data = new HospitalDBDataContext())
{
if (data.Connection.State == ConnectionState.Closed)
data.Connection.Open();
name = data.Patients.Where(x => x.BedNumber.Equals(bedNumber))
.Select(x => x.Firstname).ToString();
if (data.Connection.State == ConnectionState.Open)
data.Connection.Close();
data.Dispose();
}
return name;
}
Run Code Online (Sandbox Code Playgroud)
使这个表达式返回我需要的实际值的方法是什么?
有人会帮我理解这个for循环有什么问题吗?为什么我会超出界限异常呢?
此特定列表的容量设置为8.
public static List<Beds> BedsList = new List<Beds>(8);
private int GetFirstAvailableBed()
{
var result = 0;
for (int i = 0; i < Beds.BedsList.Capacity; i++)
{
if (Beds.BedsList[i] == null) // Here is trhowing the exception
{
result = i;
break;
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 如何替换一组我只知道第一个和最后一个的字符,中间是一个非常数的变量.
我所知道的是,这个字符串总是以&它开头,它将以;
string str = "Hello &145126451; mate!";
Run Code Online (Sandbox Code Playgroud)
怎么摆脱&145126451;?
所以期望的结果是:
string result = "Hello mate!"
Run Code Online (Sandbox Code Playgroud) 我正在尝试将具有相同属性的匿名类型强制转换为接口,但出现错误:
无法将类型为“ <> f__AnonymousType104`1 [System.Int32]”的对象强制转换为类型为“ IHasFiles”的对象。
public interface IHasFiles
{
int FileMasterId { get; }
}
var entity = new { FileMasterId = fileMasterId }.CastOrDefault<IHasFiles>();
// This is the line where I get the error
Run Code Online (Sandbox Code Playgroud) c# ×7
razor ×2
bootstrap-4 ×1
css ×1
filestream ×1
html ×1
linq ×1
linq-to-sql ×1
list ×1
loops ×1
regex ×1