从这个答案我想知道是否有一种简单的方法来扭转这种方法
public static System.ConsoleColor FromColor(System.Drawing.Color c)
{
int index = (c.R > 128 | c.G > 128 | c.B > 128) ? 8 : 0; // Bright bit
index |= (c.R > 64) ? 4 : 0; // Red bit
index |= (c.G > 64) ? 2 : 0; // Green bit
index |= (c.B > 64) ? 1 : 0; // Blue bit
return (System.ConsoleColor)index;
}
Run Code Online (Sandbox Code Playgroud)
进入
public static System.Drawing.Color FromColor( System.ConsoleColor c)
{
// ??
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法,给我一个对象列表,例如
public IEnumerable<Person> GetPerson()
{
using (myEntities ctx = new myEntities())
{
return ctx.Person.Where(x => x.Age < 50);
}
}
Run Code Online (Sandbox Code Playgroud)
在其他地方我使用这种方法
public void Main()
{
var pList = GetPerson();
pList = pList.Where(x => x.Age < 40);
Person Item = pList.FirstOrDefault(); //materialization here
}
Run Code Online (Sandbox Code Playgroud)
当我调用FirstOrDefault()select正在进行处理并且正在从数据库中检索数据时.
问题:是否using (myEntities ctx = new myEntities())达到了实现的范围?
一方面它是因为它管理数据库的选择/连接并且在物化时生成 - 另一方面它在方法之外被调用,并且可以在代码中的任何地方 - 在using指令之外
我有一个Webforms应用程序,不希望用户输入无效值.
目前我用这样的验证器控件来解决这个问题:
<asp:TextBox runat="server" ID="tbInsert"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="tbInsert" ID="rqtbInsert"
ErrorMessage="Required">
</asp:RequiredFieldValidator>
Run Code Online (Sandbox Code Playgroud)
但这样可以验证客户端的值(用户可以避免)
我是否必须为每个控件添加服务器端验证?应该怎样做?
if (!string.IsNullOrEmpty(tbInsert.Text))
{
//do sth.
}
Run Code Online (Sandbox Code Playgroud) 我有一种计算得分的方法.简化:
public static int GetScore(int v1, int v2, char v3)
{
//calculate score
return score;
}
Run Code Online (Sandbox Code Playgroud)
v1,v2并且v3是3个列表中的3个值:
List<int> Values1 = new List<int>();
List<int> Values2 = new List<int>();
List<char> Values3 = new List<char>();
//fill Values1, Values 2, Values3
Run Code Online (Sandbox Code Playgroud)
如何Select确定三个列表的每个组合并确定最高分?我想到了类似的东西:
int MaxScore = Values1.Select(x => Values2.Select(y => GetScore(x, y))).Max(); // ???
Run Code Online (Sandbox Code Playgroud)
我目前的做法
int MaxScore = 0;
foreach (int x in Values1)
{
foreach (int y in Values2)
{
foreach (char z in Values3)
{
int …Run Code Online (Sandbox Code Playgroud) 我想总结连续的正整数(在每个负值开始一个新的总和)
输入:
int[] input = new int[] { 1, 2, 3, -1, 1, 3, -2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
输出:
output = { 6,4,7 }; // 1+2+3=6, 1+3=4, 3+4=7
Run Code Online (Sandbox Code Playgroud)
我尝试过类似input.Where(x => x > 0).Sum();但不起作用的东西