我想知道Linq是否有方法来检查两个集合是否至少有一个共同的元素.我希望这样的事情:
var listA = new List<int>() { some numbers };
var listB = new List<int>() { some numbers, potentially also in list A };
bool hasSameElements = listA.hasMatchingElements(listB);
Run Code Online (Sandbox Code Playgroud)
它是否存在于Linq中,还是应该为它编写自定义方法?
我知道Intersect方法,但这不会产生整个交集吗?我只对检查两个集合是否相交感兴趣,产生整个集合似乎是浪费,特别是在较大的集合上.
我有一个任务,我希望它能在一秒钟内运行,但是如果需要的时间超过几秒钟,我想取消任务.
例如:
Task t = new Task(() =>
{
while (true)
{
Thread.Sleep(500);
}
});
t.Start();
t.Wait(3000);
Run Code Online (Sandbox Code Playgroud)
请注意,等待3000毫秒后,等待到期.超时到期或任务是否仍在运行时,任务是否被取消?
我正在学习Jeffrey Palermo着名的洋葱建筑.不是特定于这种模式,但我无法清楚地看到存储库和域服务之间的分离.我(错)了解存储库涉及数据访问和服务更多关于业务层(引用一个或多个存储库).
在许多示例中,存储库似乎具有某些类似于GetAllProductsByCategoryId或的业务逻辑GetAllXXXBySomeCriteriaYYY.
对于列表,似乎服务只是存储库中的包装器而没有任何逻辑.对于层次结构(父/子/子),它几乎是同一个问题:存储库的角色是加载完整的层次结构吗?
在C#/ XAML的Windows 8应用程序中,我有时想从非异步方法调用一个等待的方法.
实际上替换它是正确的:
public async Task<string> MyCallingMethod()
{
string result = await myMethodAsync();
return result;
}
Run Code Online (Sandbox Code Playgroud)
这样 :
public string MyCallingMethod()
{
Task.Run(async () => {
string result = await myMethodAsync();
return result;
});
}
Run Code Online (Sandbox Code Playgroud)
对我来说的好处是我可以在没有等待的情况下使用MyCallingMethod,但这是正确的吗?如果我想为MyCallingMethod传递ref参数,这可能是一个优点,因为在异步方法中不可能有ref参数.
当字符串[],_ lineParts被添加到List时,我在List中看到的只是"System.String []"需要做什么来查看列表中的实际string []值.
while (_aLine != null)
{
//split the line read into parts delimited by commas
_lineParts = _aLine.Split(new char[] { ' ', '\u000A', ',', '.', ';', ':', '-', '_', '/' },
StringSplitOptions.RemoveEmptyEntries);
//keep things going by reading the next line
_aLine = sr.ReadLine();
//words = _lineParts;
if (_lineParts != null)
{
//_words.Add(_lineParts.ToString());
wrd.Add(_lineParts.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 在该IDisposable.Dispose方法中有一种方法可以确定是否抛出异常?
using (MyWrapper wrapper = new MyWrapper())
{
throw new Exception("Bad error.");
}
Run Code Online (Sandbox Code Playgroud)
如果在using语句中抛出异常,我想在处理IDisposable对象时知道它.
是否有任何对象/方法可以提供有关浏览器,客户端的信息?
例如,我需要检测IE浏览器.我在用:
function st_IsIE()
{
if(navigator.appName.indexOf("Microsoft Internet Explorer") != -1)
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?
我需要根据键的子集从Dictionary中选择一些值(到List中).
我正在尝试使用Linq在一行代码中执行此操作,但到目前为止我发现的内容似乎很长且很笨拙.什么是最短(最干净)的方法呢?
这就是我现在拥有的(键是字符串,keysToSelect是要选择的键列表):
List<ValueType> selectedValues = dictionary1.Where(x => keysToSelect.Contains(x.Key))
.ToDictionary<String, valueType>(x => x.Key,
x => x.Value)
.Values.ToList;
Run Code Online (Sandbox Code Playgroud)
谢谢.
我只是想知道是否可以在单个Web Api中返回多个类型.例如,我希望api返回客户和订单列表(这两组数据可能相互关联,也可能不相关?
我正在尝试使用AJAX/Jquery和c#写入我的数据库.每当我将参数传递给C#代码时,它都显示为null.我正在使用visual studio在创建控制器类时生成的默认模板.任何帮助,将不胜感激!
NOte:这是我打算打电话给的休息服务.(一个普通的ASP网站......不是MVC.而且,GET Rest api非常有效.)
jQuery的/ AJAX:
var dataJSON = { "name": "test" }
$('#testPostMethod').bind("click", GeneralPost);
function GeneralPost() {
$.ajax({
type: 'POST',
url: '../api/NewRecipe',
data:JSON.stringify(dataJSON),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
}
Run Code Online (Sandbox Code Playgroud)
C#
//If I remove the [FromBody] Tag then when I click the button this method is never called.
public void Post([FromBody]string name)
{
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我稍微调整了我的代码,但仍遇到同样的问题.回顾一下,它正在加载POST方法,但它传入的是null.
C#
public class RecipeInformation
{
public string name { get; set; }
}
public void Post(RecipeInformation information)
{
}
Run Code Online (Sandbox Code Playgroud)
AJAX:
var dataJSON …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
linq ×2
ajax ×1
arrays ×1
asp.net-mvc ×1
async-await ×1
browser ×1
collections ×1
dictionary ×1
idisposable ×1
javascript ×1
jquery ×1
list ×1
task ×1
windows-8 ×1