首页 \ 问答 \ 使用该lambda中包含的匿名类型定义lambda表达式(Defining a lambda expression with an anonymous type contained within that lambda)

使用该lambda中包含的匿名类型定义lambda表达式(Defining a lambda expression with an anonymous type contained within that lambda)

我试图避免在我的lambda表达式中使用动态类型来分组集合。 该类型在编译时是匿名定义的(据我所知,这是明确的)。 我宁愿不将类型定义为一个完整的类,因为我在这个单一方法中只使用了几次。

示例代码:

Func<MyData, dynamic> dataGrouping = md => new
    {
        md.Property1,
        md.Property2,
        md.Property3
    };

var groupedData = myDataCollection.GroupBy(dataGrouping);

虽然这会编译,但由于类型是动态的,因此在组内部没有智能感知或强类型。

我不能将dataGrouping的类型指定为var,因为我在C#中,我得到的抱怨是不能将lambda表达式分配给隐式类型的局部变量

我可以用匿名类型的GetType()替换动态吗? 然后我需要在lambda中使用它之前的类型,但在我已经进入lambda本身之前,我看不到一个有用的方法来获取它。

有没有一种优雅的方式来获得这个匿名类的类型?


I'm trying to avoid a dynamic type in my lambda expression for grouping a collection. The type is defined anonymously at compile time (and unambiguously as far as I can tell). I'd rather not define the type as a full-fledged class as I'm only using it a few times in this single method.

Sample code:

Func<MyData, dynamic> dataGrouping = md => new
    {
        md.Property1,
        md.Property2,
        md.Property3
    };

var groupedData = myDataCollection.GroupBy(dataGrouping);

While this will compile, it leaves me with no intellisense or strong typing inside the group as the type is dynamic.

I can't specify the type of dataGrouping as var, because I'm in C# and I get complaints of Cannot assign lambda expression to implicitly typed local variable.

Could I replace dynamic with the result of GetType() on the anonymous type? I'd then need the type before it's used in the lambda, but I can't see a useful way to get a handle on it before I'm already into the lambda itself.

Is there an elegant way of getting the type of this anonymous class?


原文:https://stackoverflow.com/questions/3446819
更新时间:2022-05-18 19:05

最满意答案

试试这段代码:

<html>
    <head>
        <title>Checkbox</title>
    </head>
    <body>

        <input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked"/>Vanilla 
        <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate
        <input type="radio" name="flavor" id="strawberry" value="Strawberry"/>Strawberry


    <script>

         var flavorArray = ["","","Vanilla","Chocolate","Strawberry"];
         var flavorValue = document.querySelector('input[name="flavor"]:checked').value;

         console.log(flavorArray[flavorArray.indexOf(flavorValue)])
    </script>
    </body>
</html>

Try this code:

<html>
    <head>
        <title>Checkbox</title>
    </head>
    <body>

        <input type="radio" name="flavor" id="vanilla" value="Vanilla" checked="checked"/>Vanilla 
        <input type="radio" name="flavor" id="choc" value="Chocolate" />Chocolate
        <input type="radio" name="flavor" id="strawberry" value="Strawberry"/>Strawberry


    <script>

         var flavorArray = ["","","Vanilla","Chocolate","Strawberry"];
         var flavorValue = document.querySelector('input[name="flavor"]:checked').value;

         console.log(flavorArray[flavorArray.indexOf(flavorValue)])
    </script>
    </body>
</html>

相关问答

更多
  • 当您发布表单时,CF仅为您提供form范围中所选单选按钮的value 。 如果您需要ID,则应将其设置为按钮值: #rsAnswers.rraAnswer# When you post the form over, CF only gives you the value of the selected radio button ...
  • 您可以在输入的value属性中存储值( female , ... ),根据您的标记,您可以尝试以下操作: $('input[name="radio-choice-1"]').change(function(){ var genderValue = $(this).next('label').text() }) 但我建议这个:
    ...
  • var rates = document.getElementById('rates').value; rate元素是一个div ,所以它不会有一个值。 这可能是undefined的来自哪里。 checked属性将告诉您元素是否被选中: if (document.getElementById('r1').checked) { rate_value = document.getElementById('r1').value; } var rates = document.getElementById( ...
  • 我认为你可以遍历单选按钮,如果isSelected()为true,则为每个按钮循环,然后调用button.getText()来获取名称。 for (AbstractButton button : bg.getElements()) if (button.isSelected()) return button.getText(); I think you can loop through the radio buttons, for each button if isSelected ...
  • 所有Maya UI项目都是必不可少的:您必须发出命令并获得结果,没有“状态”:“按钮”或任何只是您将用于发出命令的对象的字符串名称 要获得radiocollection的状态,请在集合上调用radioCollection -q -select ,它将返回所选单选按钮的名称; 你会用它来推动你的逻辑。 string $w = `window`; string $c = `columnLayout`; string $radiocol = `radioCollection "radio buttons"`; s ...
  • 在这个显示文本的片段中(当你点击li ),我正在使用它的title属性并将h1的文本设置为等于它。 点击后的li将具有class="active" ,您可以更改它的css。 $(".s-value a").click(function(){ var heading = $(this).attr("title"); //showing the size in h1 $("#showcase").text(heading); var liIt = $(this).parent(); ...
  • 选中意味着焦点不同于复选框/单选按钮的选中/未选中状态。 使用键盘将焦点移至按钮时,您将进入此状态。 如果您想按下一个按下的按钮(切换按钮),则只需使用一个复选框并将其设置为切换按钮模式。 Selected means focus unlike the checked/unchecked state of a checkbox/radio button. You will get to this state when using the keypad to move focus to the button. ...
  • radio.checked返回一个布尔告诉你,无论是否检查了该无线电,你都不能用值链接它。 正确的方法是循环使用相同名称的所有无线电并获取已检查的无线电的值: value = ''; for( i in radio ) { if ( radio[i].checked ) value = radio[i].value; } 您必须根据自己的需要进行修改 radio.checked returns a boolean telling you, whether or not that r ...
  • 试试这段代码: Checkbox Vanilla Cho ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。