我需要用数组中的字符串过滤列表。下面的代码不会返回预期的结果。
List<searchModel> obj=//datasource is assigned from database
mystring="city1,city2";
string[] subs = mystring.Split(',');
foreach (string item in subs)
{
obj = obj.Where(o => o.property.city.ToLower().Contains(item)).ToList();
}
Run Code Online (Sandbox Code Playgroud) 让我们说我已经生成了一个数组列表,并且我希望将它们相应地分组为8.这是我如何用LINQ做的,但我想知道如何使用labmda表达式:
Random rnd = new Random();
var intArray = new List<int>();
for (int i = 0; i < 99; i++)
{
intArray.Add(rnd.Next(20, 50));
}
var randomLettersSortedAsc =
from element in intArray
group element by element % 8 into g
select g;
Run Code Online (Sandbox Code Playgroud) 我正在做我的项目,我面临的问题是组合两个锯齿状阵列并创建一个.
下面是一个例子:锯齿状的:
double[][] JaggedOne=
{
new double[] { -5, -2, -1 },
new double[] { -5, -5, -6 },
};
Run Code Online (Sandbox Code Playgroud)
以下是我的第二个:
double[][] JaggedTwo=
{
new double[] {1, 2, 3 },
new double[] { 4, 5, 6 },
};
Run Code Online (Sandbox Code Playgroud)
现在结果我想要这个:
double[][] Result =
{
{-5,-2,-1},
{-5,-5,-6},
{1,2,3},
{4,5,6},
};
Run Code Online (Sandbox Code Playgroud)
实际上第一个从XML文件和第二个加载,它是我的训练集,第二个是我在机器学习中使用的样本测试.我非常感谢您的回复和提示.
我一直在尝试过去两个小时,但我不能替换字符串\n,这就是我所做的:
Encoding enc = Encoding.ASCII;
for (int i = 0; i < numpntr; i++)
{
bw.BaseStream.Position = strt + i*var;
bw.Write(
enc.GetBytes(listView1.Items[i].SubItems[1].Text.Replace("\n","\x0A") + (new string('\0',
bytecnt - enc.GetByteCount(listView1.Items[i].SubItems[1].Text.Replace("\n" + "\x0A"))))));
}
bw.Flush();
bw.Close();
bw = null;
Run Code Online (Sandbox Code Playgroud)
无论如何将其替换为字符串?
我想取消一个线程,然后再运行另一个线程.这是我的代码:
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation token
new Thread(() => WaveFormLoop(cancelWaveForm.Token)).Start(); // starting a new thread
}
Run Code Online (Sandbox Code Playgroud)
当我调用这个方法时,第一个线程不会停止而第二个线程开始运行...
但是如果我跳过最后两行它会工作:
private void ResetMedia(object sender, RoutedEventArgs e)
{
cancelWaveForm.Cancel(); // cancel the running thread
cancelWaveForm.Token.WaitHandle.WaitOne(); // wait the end of the cancellation
cancelWaveForm.Dispose();
//some work
//cancelWaveForm = new CancellationTokenSource(); // creating a new cancellation …Run Code Online (Sandbox Code Playgroud) c# multithreading cancellationtokensource cancellation-token
我在MVC应用程序中有一个模型Auto,它具有如下属性
public string Id { get; set; }
public bool IsOOS{ get; set; }
public string Make { get; set; }
public string Model { get; set; }
[XmlElement(IsNullable = true)]
public DateTime? RegisteredDate { get; set; }
Run Code Online (Sandbox Code Playgroud)
还有一个有这个......
var a = new Auto(){
Id = someIDcomingfromServer,
IsOOS = someOOScomingFromServer,
...
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是...循环遍历这些并查看是否有任何属性现在为null.
我如何循环并查看是否有任何属性(Id,IsOOS等)包含null?
谢谢
打击两个不同的查询评估相同的结果.
我需要在不执行查询的情况下检查这些的相等性.
如何检查两个不同的LINQ查询是否相同?
var exprA = (from o in orders where o.HasPrice == true);
var exprB = (from o in orders where o.HasPrice != false);
//HasPrice is a boolean
Run Code Online (Sandbox Code Playgroud)
任何.NET解决方案或现有库的构建都将受到赞赏.
我有一个zip文件创建者,它接收一个String[]Urls,并返回一个包含所有文件的zip文件String[]
我想会有很多这方面的例子,但我似乎找不到"如何异步下载多个文件并在完成后返回"的答案
如何一次下载{n}个文件,仅在所有下载完成后返回词典?
private static Dictionary<string, byte[]> ReturnedFileData(IEnumerable<string> urlList)
{
var returnList = new Dictionary<string, byte[]>();
using (var client = new WebClient())
{
foreach (var url in urlList)
{
client.DownloadDataCompleted += (sender1, e1) => returnList.Add(GetFileNameFromUrlString(url), e1.Result);
client.DownloadDataAsync(new Uri(url));
}
}
return returnList;
}
private static string GetFileNameFromUrlString(string url)
{
var uri = new Uri(url);
return System.IO.Path.GetFileName(uri.LocalPath);
}
Run Code Online (Sandbox Code Playgroud) 我无法找到正确的语法,以便使用await with lambda表达式(匿名lambda方法).所有示例似乎都使用了使用async关键字声明的实际方法.让我来描述我想要的东西:
...code in UI thread...
Customer cust = null;
using (DataContext context = new DataContext()) // I want to run this async
{
cust = context.Customers.Single(c => c.Id == 1);
}
...continue code in UI thread...
Run Code Online (Sandbox Code Playgroud)
为了在数据库查询期间不阻塞UI线程,我会写:
...code in UI thread...
Customer cust = null;
Task.Run(() =>
{
using (DataContext context = new DataContext())
{
cust = context.Customers.Single(c => c.Id == 1);
}
});
...continue code in UI thread...
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用,因为UI线程将在任务启动后继续.我不能Wait()上Task,因为这会阻止用户界面线程.简单地await在前面添加Task.Run()不编译.我能想到的最好的事情是: …