我试图将列表拆分成一系列较小的列表.
我的问题:我拆分列表的功能不会将它们拆分成正确大小的列表.它应该将它们分成大小为30的列表,而是将它们分成大小为114的列表?
如何使我的功能将列表拆分为X个大小为30或更小的列表?
public static List<List<float[]>> splitList(List <float[]> locations, int nSize=30)
{
List<List<float[]>> list = new List<List<float[]>>();
for (int i=(int)(Math.Ceiling((decimal)(locations.Count/nSize))); i>=0; i--) {
List <float[]> subLocat = new List <float[]>(locations);
if (subLocat.Count >= ((i*nSize)+nSize))
subLocat.RemoveRange(i*nSize, nSize);
else subLocat.RemoveRange(i*nSize, subLocat.Count-(i*nSize));
Debug.Log ("Index: "+i.ToString()+", Size: "+subLocat.Count.ToString());
list.Add (subLocat);
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
如果我在144的列表上使用该函数,那么输出是:
指数:4,大小:120
指数:3,大小:114
索引:2,大小:114
索引:1,大小:114
索引:0,大小:114
我DropDownListFor在我的MVC3应用程序中遇到了麻烦.我能够使用StackOverflow来弄清楚如何让它们出现在View上,但现在我不知道在提交时如何捕获视图模型中相应属性中的值.为了使其工作,我必须创建一个具有ID和value属性的内部类,然后我必须使用a IEnumerable<Contrib>来满足DropDownListFor参数要求.但是,现在,MVC FW应该如何将在此下拉列表中选择的值映射回我的视图模型上的简单字符串属性?
public class MyViewModelClass
{
public class Contrib
{
public int ContribId { get; set; }
public string Value { get; set; }
}
public IEnumerable<Contrib> ContribTypeOptions =
new List<Contrib>
{
new Contrib {ContribId = 0, Value = "Payroll Deduction"},
new Contrib {ContribId = 1, Value = "Bill Me"}
};
[DisplayName("Contribution Type")]
public string ContribType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中,我将下拉列表放在页面上,如下所示:
<div class="editor-label">
@Html.LabelFor(m => m.ContribType)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.ContribTypeOptions.First().ContribId,
new …Run Code Online (Sandbox Code Playgroud) 什么是与此方法等效的JavaScriptC#:
var x = "|f|oo||";
var y = x.Trim('|'); // "f|oo"
Run Code Online (Sandbox Code Playgroud)
C#仅在字符串的开头和结尾处修剪所选字符!
我正在尝试调整图像大小,同时保留原始图像的宽高比,这样新图像看起来不会被压扁.
例如:
将150*100图像转换为150*150图像.
高度的额外50个像素需要用白色背景颜色填充.
这是我正在使用的当前代码.
它适用于调整大小,但更改原始图像的宽高比会压缩新图像.
private void resizeImage(string path, string originalFilename,
int width, int height)
{
Image image = Image.FromFile(path + originalFilename);
System.Drawing.Image thumbnail = new Bitmap(width, height);
System.Drawing.Graphics graphic =
System.Drawing.Graphics.FromImage(thumbnail);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(image, 0, 0, width, height);
System.Drawing.Imaging.ImageCodecInfo[] info =
ImageCodecInfo.GetImageEncoders();
EncoderParameters encoderParameters;
encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,
100L);
thumbnail.Save(path + width + "." + originalFilename, info[1],
encoderParameters);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我想填充图像而不是裁剪
我已经阅读了很多关于此的问题,但我找不到一个足够快的问题.我认为有更好的方法可以将大量行插入MySQL数据库
我使用以下代码将100k插入我的MySQL数据库:
public static void CSVToMySQL()
{
string ConnectionString = "server=192.168.1xxx";
string Command = "INSERT INTO User (FirstName, LastName ) VALUES (@FirstName, @LastName);";
using (MySqlConnection mConnection = new MySqlConnection(ConnectionString))
{
mConnection.Open();
for(int i =0;i< 100000;i++) //inserting 100k items
using (MySqlCommand myCmd = new MySqlCommand(Command, mConnection))
{
myCmd.CommandType = CommandType.Text;
myCmd.Parameters.AddWithValue("@FirstName", "test");
myCmd.Parameters.AddWithValue("@LastName", "test");
myCmd.ExecuteNonQuery();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这需要100k行约40秒.我怎样才能更快或更高效?
通过DataTable/DataAdapter或一次插入多行可能会更快:
INSERT INTO User (Fn, Ln) VALUES (@Fn1, @Ln1), (@Fn2, @Ln2)...
Run Code Online (Sandbox Code Playgroud)
由于安全问题,我无法将数据加载到文件和MySQLBulkLoad它.
下午,我需要找出将阵列拆分成较小"块"的最佳方法.
我正在传递大约1200个项目,并且需要将这些项目拆分为更容易处理的100个组,然后我需要将它们传递给处理.
有人可以提出一些建议吗?
假设我有一个包含重复值的List,我想删除重复项.
List<int> myList = new List<int>(Enumerable.Range(0, 10000));
// adding a few duplicates here
myList.Add(1);
myList.Add(2);
myList.Add(3);
Run Code Online (Sandbox Code Playgroud)
我找到了3种方法来解决这个问题:
List<int> result1 = new HashSet<int>(myList).ToList(); //3700 ticks
List<int> result2 = myList.Distinct().ToList(); //4700 ticks
List<int> result3 = myList.GroupBy(x => x).Select(grp => grp.First()).ToList(); //18800 ticks
//referring to pinturic's comment:
List<int> result4 = new SortedSet<int>(myList).ToList(); //18000 ticks
Run Code Online (Sandbox Code Playgroud)
在SO的大多数答案中,Distinct方法显示为"正确的",但HashSet总是更快!
我的问题:当我使用HashSet方法时,有什么我必须要注意的,还有另一种更有效的方法吗?
我正在尝试实现一个连接多个Lists 的方法
List<string> l1 = new List<string> { "1", "2" };
List<string> l2 = new List<string> { "1", "2" };
List<string> l3 = new List<string> { "1", "2" };
var result = Concatenate(l1, l2, l3);
Run Code Online (Sandbox Code Playgroud)
但我的方法不起作用:
public static IEnumerable<T> Concatenate<T>(params IEnumerable<T> List)
{
var temp = List.First();
for (int i = 1; i < List.Count(); i++)
{
temp = Enumerable.Concat(temp, List.ElementAt(i));
}
return temp;
}
Run Code Online (Sandbox Code Playgroud) 我有简单的二叉搜索树
public class BNode
{
public int item;
public BNode right;
public BNode left;
public BNode(int item)
{
this.item = item;
}
}
public class BTree
{
private BNode _root;
private int _count;
private IComparer<int> _comparer = Comparer<int>.Default;
public BTree()
{
_root = null;
_count = 0;
}
public bool Add(int Item)
{
if (_root == null)
{
_root = new BNode(Item);
_count++;
return true;
}
else
{
return Add_Sub(_root, Item);
}
}
private bool Add_Sub(BNode Node, int Item)
{ …Run Code Online (Sandbox Code Playgroud) 我需要在mySQL数据库中将一个巨大的CSV文件插入到2个具有1:n关系的表中.
CSV文件每周发送一次,大约1GB,需要附加到现有数据.其中每个表都有一个自动增量主键.
我试过了:
还有什么建议?
让我们说简化这是我的数据结构:
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<string> Codes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要从csv插入到这个数据库中:
User (1-n) Code
+---+-----+-----+ +---+---+-----+
|PID|FName|LName| |CID|PID|Code |
+---+-----+-----+ +---+---+-----+
| 1 |Jon | Foo | | 1 | 1 | ed3 |
| 2 |Max | Foo | | 2 | 1 | wst |
| 3 |Paul | Foo | | …Run Code Online (Sandbox Code Playgroud) c# ×9
list ×2
mysql ×2
split ×2
arrays ×1
binary-tree ×1
console ×1
distinct ×1
ienumerable ×1
insert ×1
javascript ×1
performance ×1
string ×1
trim ×1