您好我正在寻找一种让All方法在异步模式下运行的方法.事实上,我正试图想办法在LINQ中何时需要它来使用异步.一些linq方法也有异步定义,我不太明白为什么不是所有方法都有异步定义,所以也许有人可以为我清理.
与我有关,试图让所有运行异步
版本1
async Task<bool> IHrtbProfileValidator.ValidateHrtb(UserHrtbProfileDTO dto_Hrtb)
{
var x = _validator.All(async (ck) => await ck.ValidateHrtb(dto_Hrtb));
return x;
}
Run Code Online (Sandbox Code Playgroud)
版本2
var x = _validator.All((ck) => await ck.ValidateHrtb(dto_Hrtb));
Run Code Online (Sandbox Code Playgroud)
版本3
var x = _validator.All(async (ck) => await ck.ValidateHrtb(dto_Hrtb).Result);
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的
我的想法是,我有一个IValidator接口,多个验证器类通过该接口实现每个负责的自己的验证相关逻辑.在MainValidator类中,我只是想调用All方法来验证IValidator列表.
谢谢你们
我正在寻找一个异步,.Where()但找不到一个,所以经过一些研究我创造了一个.
public static class LinqExtension
{
public static async Task<IEnumerable<T>> WhereAsync<T>(this IEnumerable<T> source, Func<T, Task<bool>> @delegate)
{
var tasks = source.Select(async t => new
{
Predicate = await @delegate(t).ConfigureAwait(false),
Value = t
}).ToList();
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
IEnumerable<T> typeList = results.Where(pred => pred.Predicate).Select(val => val.Value);
return typeList;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它时,我得到运行时错误
无法将隐式bool转换为Task,是的,它是正确的
这就是我尝试过的方式
var q = await context.StockHistories.WhereAsync(x => x.ProductId == productId);
Run Code Online (Sandbox Code Playgroud)
我试过了
context.StockHistories.WhereAsync(Task.Run(() => { x => x.ProductId == productId; }));
Run Code Online (Sandbox Code Playgroud)
但得到
只有赋值,调用,递增,递减和新对象表达式才能用作语句
可以请有人提供解决方案,并解释我做错了什么?
我正在尝试使用 span 元素来实现一个技巧。附有一张图片,以便我可以更容易地解释。
\n\n左当前行为 →右期望行为
\n\n\n\n所以我的想法是我需要在 span 元素中仅显示 3 行文本。超过 3 个时,我需要只显示 3 个,最后一个单词是 [...] (当悬停时,会看到剩余的文本 - 这部分已完成)。
\n\n我在 css 方面尝试过类似的方法
\n\nspan.itemMessage\n{\n display: block; /* or inline-block */\n height: 37px;\n overflow: hidden;\n position: relative;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n是的,它工作正常,每次只显示 3 行,但如果我有更多,我不知道如何处理我正在谈论的情况。
\n\n我正在考虑将文本的高度与跨度的高度进行比较,以及文本的高度是否大于某些 .js,但我不知道该怎么做。
\n\n我有40条消息可以显示,并且“”之间的文章名称可以是任意长度。
\n\n var trimedMessage = message;\n\n //try {\n // var first = message.split(\'\xe2\x80\x9c\');\n // var second = first[1].split(\'\xe2\x80\x9d\');\n // var article = second[0];\n // if (article.length > \n // if …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以使用system.reflection转换为精确类型,这样你就可以避免进行显式转换
(System.DateTime)
Run Code Online (Sandbox Code Playgroud)
例如
假设我有一个像这样的词典
Dictionary<propName, Dictionary<object, Type>>
Run Code Online (Sandbox Code Playgroud)
并假设我迭代一个对象道具列表
foreach (var prop in @object.GetType().GetProperties())
{
object propValue = propInfo.GetValue(@object, null);
string propName = propInfo.Name;
Dictionary<object, Type> typeDictionary = new Dictionary<object, Type>();
Type propType = propInfo.GetValue(@object, null).GetType();
typeDictionary[propValue ] = propType ;
propsDictionary[propInfo.Name] = propValue;
}
Run Code Online (Sandbox Code Playgroud)
我想做类似的事情,使用类似的东西投射到精确类型
// this part is only for guidelines
// it should obtain the exact Type
// ... but it returns a string of that type Namespace.Type
Type exactType = Dictionary[enumOfPropName][someValue]
// this part should know the …Run Code Online (Sandbox Code Playgroud) .net ×3
c# ×3
linq ×2
async-await ×1
casting ×1
css ×1
html ×1
javascript ×1
jquery ×1
reflection ×1