我执行的查询将结果集带回到query对象中.为了构建一个表示查询结果的对象,我做了:
var customer = new Customer
{
CustomerAddress = $"{query.CustomerAddress} {query.City} {query.State} {query.Zip} {query.Country}",
CustomerPhone = $"{query.PhoneNumber}"
};
Run Code Online (Sandbox Code Playgroud)
现在,如果query填充了所有属性,则此工作正常.但是我意识到如果我只有Country四个额外空格的值,那么就放在国名之前.
在这种情况下customer.CustomerAddress成为" United Kingdom".
调试时,我在查询上放置一个断点,我可以看到前面没有空格query.Country,它包含United Kingdom.
我知道我可以Trim()用来修剪任何空白区域,但我想知道为什么我会得到这种行为.
我想避免任何白色空间
在查询结果集中为空.
我有以下字符串
string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB\SubdirectoryC\Test.jpg";
Run Code Online (Sandbox Code Playgroud)
我正在尝试删除部分字符串,所以最后我想留下
string a = @"\\server\MainDirectory\SubDirectoryA\SubdirectoryB";
Run Code Online (Sandbox Code Playgroud)
所以目前我正在做
string b = a.Remove(a.LastIndexOf('\\'));
string c = b.Remove(b.LastIndexOf('\\'));
Console.WriteLine(c);
Run Code Online (Sandbox Code Playgroud)
这给了我正确的结果.我想知道是否有更好的方法来做到这一点?因为我必须在很少的地方做这件事.
注:该SubdirectoryC长度将是未知的.因为它是由用户输入的数字/字母组成的
考虑以下对象
public class Foo
{
public bool RecordExist { get; set; }
public bool HasDamage { get; set; }
public bool FirstCheckCompleted { get; set; }
public bool SecondCheckCompleted { get; set; }
//10 more bool properties plus other properties
}
Run Code Online (Sandbox Code Playgroud)
现在我想要实现的是将属性值设置为除和之外的true所有bool属性.为实现这一目标,我已经开始创建以下方法.RecordExistHasDamage
public T SetBoolPropertyValueOfObject<T>(string[] propNames, Type propertyType, object value)
{
PropertyInfo[] properties = typeof(T).GetProperties();
T obj = (T)Activator.CreateInstance(typeof(T));
if(propNames.Length > 0 || propNames != null)
foreach (var property in properties)
foreach (string pName …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的方法,它查询数据库并返回一个值.代码如下:
public List<int?> TravelTime()
{
List<int?> items = new List<int?>();
Induction induction = new Induction();
using (var dbContext = new MyEntites())
{
var query = dbContext.MyTable.Select(a => a.Travel_Time).Take(1);
foreach (var item in query)
{
induction.TravelTime = item;
items.Add(induction.TravelTime);
}
}
return items;// Value here is 8
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下代码对此方法进行单元测试:
[TestMethod]
public void Check_Travel_Time_Test()
{
//Arrange
InductionView vModel = new InductionView();
Induction induction = new Induction();
List<int?> actual = new List<int?>();
induction.TravelTime = 8;
actual.Add(induction.TravelTime);
//Act
var expected = vModel.TravelTime();
//Assert
Assert.AreEqual(expected, …Run Code Online (Sandbox Code Playgroud) 我有一个字符串
string s = "abc; abc bla bla ;;;;; bla bla";
Run Code Online (Sandbox Code Playgroud)
我想;用一个替换除了第一个之外的所有:.我可以得到如下计数:
int t = s.Where(e => e.ToString() == ";").Count();
Run Code Online (Sandbox Code Playgroud)
如果我做了s.Replace(';', ':');所有的;替换:.有人可以告诉我如何实现这一目标.