Tag: lambda

如何将lambda传递给Razor辅助方法?

我有一个razor助手方法需要接受一个Func ,它将返回一些HTML内容打印出来。 这是我原来的: @helper node(string title, Func descriptions) { …. @descriptions() …. } @node(“title”, new Func(() => { return new HelperResult( @ “desc1” “desc2” ); })) 不幸的是,我的文字永远不会打印出来。 也没有错误。 所以我学习了内联帮助器,并将调用方法更改为: @node(“title”, @ “desc1” “desc2” ) 但是现在我收到编译错误说 “委托’System.Func’不接受1个参数”。 但我不是在传递任何论点。 因此,如果我将其更改为Func然后使用@descriptions(null)调用它,我会收到以下错误: “如果没有先将lambda表达式转换为委托或表达式树类型,则不能将lambda表达式用作动态调度操作的参数” 我确定我的某些地方出了问题,但我不确定它是什么。 编辑 :我想我可能已经解决了这个问题,但它引入了一些其他问题。 我做的是在传递给动态方法之前施放lambda。 我猜这就是错误试图说的: @node(“title”, ((Func)(@ “desc1” “desc2” )) 这有效,它可以正确打印出span标签。 不幸的是,我必须在调用此Func时传入一​​个无用的参数。 现在我遇到的问题是我的真正function不仅仅是写一些跨度。 它更像是这样的: @node(“title”, ((Func)(@ […]

如何在返回集合的lambda中使用async

我有一个异步“上游”的方法。 我正在尝试遵循最佳实践并在堆栈中一直进行qith异步。 在MVC中的Controller操作中,我可以预见到死锁问题如果我依赖.Result()。 将Controller操作更改为异步似乎是要走的路,尽管问题是在lambda中多次调用async方法。 我如何等待返回多个结果的lamda? public async Task GetLotsOfStuff() { IEnumerable things= previouslyInitialisedCollection .Select(async q => await GetDetailAboutTheThing(q.Id))); return Json(result, JsonRequestBehavior.AllowGet); } 您可以看到我尝试使lambda异步,但这只是一个编译器exception: 无法转换源类型 System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task到目标类型System.Collections.Generic.IEnumerable 我在哪里错了?

Lambda参数在后面的范围内访问字段时与类字段冲突

在名称方面我的想象力很弱,所以我经常发现自己在代码中重用了标识符。 这导致我遇到这个特定的问题。 这是一些示例代码: public delegate void TestDelegate(int test); public class Test { private int test; private void method(int aaa) { TestDelegate del = test => aaa++; test++; } public static void Main() { } } 以下是编译错误(由ideone输出): prog.cs(11,3): error CS0135: `test’ conflicts with a declaration in a child block prog.cs(9,22): (Location of the symbol related to […]

尝试强制转换匿名对象时出错,Razor

我正在尝试强制转换一组匿名对象,其中每个对象如下所示: new {type=”internal”,title=”Linktitle”,target=”_blank”,link=”http://www.google.se”} 我已经声明了一个类“链接”,应该对其进行匿名对象的转换 class Link{ public string type {get;set;} public string target {get;set;} public string title {get;set;} public string link {get;set;} } 现在我试图像这样投射物体 List links = Model.relatedLinks.Select(l => new Link{type=l.type,target=l.target,title=l.title,link=l.link}).ToList(); 然后我得到了错误 Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree […]

深度> 1的成员访问的表达式树

public class Job { public string Name { get; set; } public int Salary { get; set; } } public class Employee { public string Name { get; set; } public Job Job { get; set; } } 如果我想创建一个成员访问Employee.Name的表达式树,这就是我所做的: var param = Expression.Parameter(type, “x”); var memberAccess = Expression.PropertyOrField(param, memberName); return Expression.Lambda<Func>(memberAccess, param); 对Employee.Job.Salary的成员访问权限相当于什么?

获取XML上的嵌套元素(使用Lambda)并设置为List

我有一个嵌套List的XML,我需要在我的类对象中的所有属性。 类: public class Process { public Process() { Progresses = new List(); } public string Code{ get; set; } public List Progresses { get; set; } } public class Progress { public string Text { get; set; } public string ProgressDate { get; set; } } XML: 8754639647985 09/11/2013 Lorem lalal asdf 10/11/2015 Lorem […]

由lambda排除另一个集合

这是我的类型: public class myType { public int Id { get; set; } public string name { get; set; } } 这种类型有2个集合: List FristList= //fill ; List Excludelist= //fill; 我需要从FristList排除Excludelist ,如下所示: List targetList = FirstList.Where(m=>m.Id not in (Excludelist.Select(t=>t.Id)); 关于上述查询的确切lambda表达式,您有什么建议?

Linq lambda表达多对多表选择

我有三张桌子,其中两张是多对多的关系。 图片: 这是中间mm表格中的数据: 编辑:直到这里,我得到适当的4行,但他们都是相同的结果(我知道我需要4行,但有不同的结果) return this._mediaBugEntityDB.LotteryOffers .Find(lotteryOfferId).LotteryDrawDates .Join(this._mediaBugEntityDB.Lotteries, ldd => ldd.LotteryId, lot => lot.Id, (ldd, lot) => new Lottery { Name = lot.Name, CreatedBy = lot.CreatedBy, ModifiedOn = lot.ModifiedOn }).AsQueryable(); 我的问题是,如何才能通过多个表格检索所有彩票?我只有LotteryOfferId? 我想要实现的是通过LotteryDrawDateId从彩票表中获取数据。 首先我使用LotteryOfferId从中间表获取DrawDates,并且通过中间表我得到drawDateIds以在LotteryDrawDate表中使用它们。 从那张桌子我需要在LotteryDrawDate表中通过LotteryId来修复Lottey表。 我通过普通的SQL得到这个(LotteryOffersLotteryDrawDates是DB中的中间表,在模型中看不到): 选择姓名,Lotteries.CreatedBy,Lotteries.ModifiedOn,计数(Lotteries.Id)作为Lotteries的TotalDrawDates加入LotteriesDurreDates Lottery.Id = LotteryDrawDates.LotteryId加入LotteryOffersLotteryDrawDates on LotteryDrawDates.Id = LotteryOffersLotteryDrawDates.LotteryDrawDate_Id其中LotteryOffersLotteryDrawDates.LotteryOffer_Id = 19 group by Name ,Lotteries.CreatedBy,Lotteries.ModifiedOn 但是Linq的故事不同:P 我想用lambda表达式做到这一点。 谢谢

使用嵌套的lambda表达式集合来创建对象图

我有兴趣利用lambda表达式来创建属性选择器树。 使用场景是我们有一些代码对对象图进行一些递归reflection,并且为了限制递归的范围,我们当前正在使用Attributes来标记应该遍历哪些属性。 ie获取对象的所有修饰属性,如果该属性是具有修饰属性的引用类型,则对每个属性重复。 使用属性的限制是您只能将它们放在您控制源的类型上。 lambda表达式树允许在任意类型的公共成员上定义范围。 使用速记方式定义这些表达式会很方便,这反映了对象图的结构。 最终,我喜欢这样的事情: Selector selector = new [] { (t => Property1), (t => Property2) { p => NestedProperty1, p => NestedProperty2 } }; 现在,我能做的最好的事情就是为每个节点声明一个实例,如下所示: var selector = new Selector() { new SelectorNode(t => Property1), new SelectorNode(t => Property2) { new SelectorNode(p => NestedProperty1), new SelectorNode(p => NestedProperty2) }, }; 这段代码没有任何问题,但你必须明确地为每个节点写出类型参数,因为编译器无法推断出类型参数。 […]

通用表达式抽象问题

我有以下方法SetMapping() ,用于使用表达式定义一些映射设置。 public class AggregateMap { protected Expression<Func<IUpdateConfiguration, object>> graphMapping; protected void SetMapping(Expression<Func<IUpdateConfiguration, object>> mapping) { graphMapping = mapping; } } 调用代码示例: SetMapping(map => map.OwnedCollection(root => root.ChildEntities)); 上面的工作很好,但我想通过提供SetOwnedCollectionMapping()进一步抽象这个方法。 这意味着调用代码可以提供更基本的表达式。 进一步抽象的方法: protected void SetOwnedCollectionMapping(Expression<Func<TDataEntity, ICollection>> mapping) { graphMapping = map => map.OwnedCollection(mapping); } 调用代码示例: SetOwnedCollectionMapping(root => root.ChildEntities); 然后,通过在Entity Framework DbContext实例上调用以下方法,将此graphMapping字段用于外部库(RefactorThis.GraphDiff): public static void UpdateGraph(this DbContext context, […]