小编era*_*zap的帖子

Enumerable.Select的lambda表达式

我想弄清楚如何开始使用linq和lambda表达式.

首先,如果有人可以指导我学习一些好的教程,那将非常感激.

其次:

我正在尝试使用Select方法选择所有等于特定值的值.

我注意到select可以定义为

Select<TSource,TResult>(...lambda expression...)  
Run Code Online (Sandbox Code Playgroud)

现在为此我想选择所有等于5的数字.

int[] numbers = { 1, 2, 3, 4, 5, 5, 5, 6, 7, 8 };
IEnumerable<int> res = numbers.Select( x=>5 );    
Run Code Online (Sandbox Code Playgroud)

这不起作用,我只是不明白这是如何工作的.在什么情况下,我应该定义TSourceTResult,他们会在这种情况下怎么办?

提前致谢!

c# linq lambda

2
推荐指数
1
解决办法
2万
查看次数

用于遍历数组的lambda表达式

我是一个lambda noob

我正在寻找一种方法使用匿名方法来总结我的项目中的计数变量的结果

class SomeObject
{
    public int Count{get;}
}

SomeObject [] items = new SomeObject[]{......};  
Run Code Online (Sandbox Code Playgroud)

我正在寻找一个lambda表达式来总结并返回所有计数的数量

Func<SomeObject[],int> counter =  // the lambada i don't know how to write.
Run Code Online (Sandbox Code Playgroud)

感谢任何帮助和一些好的教程的参考

我不想发布另一个困境,扩展都很好,但如果我需要执行一个不是为Sum,Where,Select ... ext等集合内置的进程.

例如 :

     string description = string.empty; 
     foreach(var provider in Providers)
     {
            description += provider.Description ;
     }
     return decapitation .
Run Code Online (Sandbox Code Playgroud)

iv'e将它封装在一个Func委托中,但是我需要使用lambda表达式引用该委托给匿名方法,该表达式预先形成了上面的代码,我无法弄清楚这样做的语法.

一般来说,我正在寻找一种使用lambda表达式编写foreach循环的方法

(fyi代码是示例性的,没有实际用途).

c# lambda delegates

2
推荐指数
1
解决办法
1926
查看次数

同时在几个执行线程上开始操作

我遇到了一个面试问题,要求我模拟一场赛车比赛.

这一般都没有问题,我所做的只是调用car.Race()方法,我建立的循环直到汽车到达目的地(这不重要!)

什么是棘手的是使汽车"竞赛"的所有10个线程(每辆车的线程)同时调用Car.Race()而不是一个接一个地调用

  Action<Car,ManualResetEvent> raceDel = (car,handel) =>{
        handel.WaitOne();
        car.Race();
  }

  main()
  {
     ManualResetEvent [] handels = new ManualResetEvent[num_cars];
     Car [] cars = new Car[num_cars];
     for(int i = 0 ; i < num_cars ; i++)
     {
        handels[i] = new ManualResetEvent(false);
        cars[i] = new Car();
        raceDel.BeginInvoke(cars[i],handels[i],null,null);
     } 
      // the question lies here  , i'm looking for something along the lines of 
        WaitHandel.SetAll(handels); // which no such method exists :)

  }   
Run Code Online (Sandbox Code Playgroud)

线程1可以启动,并且在线程5开始执行之前汽车将到达课程的结尾.

我所做的是向每个线程发送一个manualresetevent并在其上调用waitOne()现在的问题是框架中的哪个元素可以同时发出整个ManualResetEvent数组.

WaitHandle有一个方法WaitHandle.WaitAll(MRE_Array),它会等待一个MRE数组来调用send,我正在寻找相反的东西,它会在整个manualresetevent数组上调用Set().

有任何想法吗 ?

感谢提前eran,任何可以解决问题的工作或其他同步对象也会很受欢迎.

c# multithreading synchronization

2
推荐指数
1
解决办法
154
查看次数

依赖属性继承

我需要我的控件从Grid类型的祖先那里继承UIElement.IsEnabledProperty(可以选择Window或其他任何我可以用来包装我的网格的元素)

CS:在下面,我重写UIElement.IsEnabledProperty的元数据,并使用Change和Coerce委托对其进行设置。

    static PipeControl()
    {
        PipeControl.IsEnabledProperty.OverrideMetadata(typeof(PipeControl), new FrameworkPropertyMetadata(false, OnIsEnabledPropertyChanged, OnIsEnabledPropertyCoerce));
    }

    private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var isEnabled = (bool)e.NewValue;
    }

    private static object OnIsEnabledPropertyCoerce(DependencyObject d, object baseValue)
    {
        var valueSource = DependencyPropertyHelper.GetValueSource(d, PipeControl.IsEnabledProperty);

        var pipeContorl = d as PipeControl;
        if (pipeContorl == null) return baseValue;

        return (bool)baseValue && pipeContorl.IsMyPipe;
    }
Run Code Online (Sandbox Code Playgroud)

XAML:

    <Grid IsEnabled="{Binding IsMyCondition , Mode=OneWay}">        
         <game:PipeControl Grid.Row="2"  />            
         <game:PipeControl  Grid.Row="2" Grid.Column="1" />
    </Grid> 
Run Code Online (Sandbox Code Playgroud)

每次IsMyCondition更改时,每个PipeContorl中都会调用一次OnIsEnabledPropertyCoerce,从不调用OnIsEnabledPropertyChanged,OnIsEnabledProerty Coerce中的ValueSource为“ Default”(显示Coerce始终获取默认的假值)。

我必须以需要使用继承的方式错过某些东西,我希望值源被“继承”,并调用OnIsEnabledPropertyChanged。

wpf inheritance dependency-properties custom-controls

2
推荐指数
1
解决办法
3508
查看次数

ScrollViewer - 子元素的指示滚动到视图中

是否有一个事件在孩子滚动进入视图时被提出并指示孩子被实现了什么?

当然有ScrollChanged事件,但它没有向我提供任何关于滚动到视图中的元素的指示.

提前致谢.

编辑:

我曾尝试连接到ScrollViewer的RequestBringIntoView事件,但它永远不会到达.或者我也在包含这些项目的StackPanel上尝试了相同的操作:

XAML :

     <ScrollViewer RequestBringIntoView="ScrollViewer_RequestBringIntoView" >
        <StackPanel RequestBringIntoView="StackPanel_RequestBringIntoView">
            <Button Content="1" Height="20"/>
            <Button Content="2" Height="20"/>
            <Button Content="3" Height="20"/>
            <Button Content="4" Height="20"/>
            <Button Content="5" Height="20"/>
            <Button Content="6" Height="20"/>
            <Button Content="7" Height="20"/>
            <Button Content="8" Height="20"/>
            <Button Content="9" Height="20"/>
            <Button Content="10" Height="20"/>
            <Button Content="11" Height="20"/>
            <Button Content="12" Height="20"/>
            <Button Content="13" Height="20"/>
            <Button Content="14" Height="20"/>
            <Button Content="15" Height="20"/>
            <Button Content="16" Height="20"/>
            <Button Content="17" Height="20"/>
            <Button Content="18" Height="20"/>
            <Button Content="19" Height="20"/>
            <Button Content="20" Height="20"/>
            <Button Content="21" Height="20"/>
            <Button Content="22" Height="20"/>
            <Button Content="23" …
Run Code Online (Sandbox Code Playgroud)

wpf scrollviewer

2
推荐指数
1
解决办法
5752
查看次数

C#从IEnumerable集合中删除重复的对象

我有一个主要的IEnumerable集合,另一个较小的集合女巫包含一些重复的大集合,

   IEnumerable<T> all_objects ;
   IEnumerable<T> some_of_the_objects ;
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种"更好看"的方法来从all_objects中删除some_of_the_objects中的所有对象,而不必遍历较小的集合

  foreach(T _object in some_of_the_objects)
  {
      all_objects.Remove(_object); 
  }
Run Code Online (Sandbox Code Playgroud)

c# collections ienumerable

1
推荐指数
1
解决办法
2133
查看次数

c ++ init char指针为null

我在我的类中有一个指向char的指针,该指针稍后将用于引用我不知道的大小的char数组,因此我想将它作为char*引用它

我似乎无法为其赋值null.

它仅显示为Bad Ptr异常

我怎么能初学它所以在我的ctor我可以为"char数组"分配空间这是我想要完成的,如果用c写的话似乎很简单.

     ctor
     {
          if( m_data != NULL )
                   m_data = new char[m_size];
     }
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
418
查看次数

并非所有内容都可以从按钮派生的自定义控件上单击

我有一个"立方体"(Dice)控件,它来自Button

立方体:

public class Cube : Button
{        
    public Cube()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(Cube), new FrameworkPropertyMetadata(typeof(Cube)));                                
    }
    ...... // Stuff
}
Run Code Online (Sandbox Code Playgroud)

模板(一般):

<ControlTemplate TargetType="{x:Type local:Cube}" x:Key="CubeControlTemplate">
         <Border>                
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="40"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <Border>
                        <Grid>
                            .......
                        </Grid>                          
                    </Border>

                    <Border Grid.Column="2">
                        <Grid>
                            .......
                        </Grid>                          
                    </Border>    
              <Grid> 
        </Border> 
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

它看起来像什么:

在此输入图像描述

黄色标记显示它只能在内容后面点击,只有当你真正针对按钮被"隐藏"的点击时才会点击.

任何想法为什么会这样?

wpf xaml controltemplate

1
推荐指数
1
解决办法
716
查看次数

使用基于objecttype的不同itemcontainerstyles

我有一个带有集合的列表框

ObservableCollection<BaseObject> _baseObjects;
public ObservableCollection<BattlegroundBaseObject> BaseObject
{
    get { return _baseObjects?? (_baseObjects= new ObservableCollection<BaseObject>()); }
} 
Run Code Online (Sandbox Code Playgroud)

该集合有两个来自BaseObject的子项.一个是另一个图像的路径..更多的是来了

我现在需要两个基于孩子的不同的ItemContainerStyles

<ListBox.ItemContainerStyle>

     <Style BasedOn="ListBoxItem" TargetType="ListBoxItem"  x:Name="ListBoxPathLineStyle">
        <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="ListBoxItem">
                     <Path Stroke="{Binding ObjectColor}" Data="{Binding PathGeometryData}" />                        
               </ControlTemplate>
             </Setter.Value>
      </Setter>

       <!-- Alternative Template for other type -->
      <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="ListBoxItem">
                   <Image Source="howTheHellCares.png"/>                                           
               </ControlTemplate>
           </Setter.Value>
      </Setter>               
   </Style> 
</ListBox.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

目前较低的二传手总是被带走,但我需要区别对待......某人知道怎么做?

wpf styles listboxitem

1
推荐指数
1
解决办法
1717
查看次数

未知提供者:$ state

你好,我通过$ injector服务有一些依赖注入的问题.
我是棱角分明的新手,所以我将从IoC容器的角度解释我是如何看待它的.

首先关闭问题:$ injector无法解决$ state.

core.js

(function() {
    'use strict';
     angular.module('app.core',['ui.router'])
         .config(function($stateProvider,$state){
           // ......
     });
}());
Run Code Online (Sandbox Code Playgroud)

这是错误:

  Uncaught Error: [$injector:modulerr] Failed to instantiate module app due      to:
  Error: [$injector:modulerr] Failed to instantiate module app.core due to:
  Error: [$injector:unpr] Unknown provider: $state
Run Code Online (Sandbox Code Playgroud)

我不认为堆栈跟踪在这里有很多相关性...但是,以防万一我将它发布在问题的底部:

Index.html:我只是想显示我引用我的.js文件的位置和顺序.

    <head>
    ....  <!-- pointing out that my .js are not here --> 
    </head>
    <body>

        <script src="bower_components/jquery/dist/jquery.js"></script>
        <script src="bower_components/angular/angular.js"></script>
        <script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>

        <script src="components/core/lib/core.js"></script>
        **<!-- relevant to the EDIT part below -->
        <script src="components/something/something.js"></script>** 
    </body>
Run Code Online (Sandbox Code Playgroud)

据我了解angular的供应商食谱:

$stateProvider …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-ui-router angular-providers

1
推荐指数
1
解决办法
4098
查看次数

DataAdapter.Update()不会更新DB中的数据

我有一个任务,要求我更新northwind数据库,我做了一些像教程所说的如下

我填写DataTable使用The DataAdapter.Fill(table).

Delete,Insert,Update使用构建命令CommangBuilder

SqlDataAdapter adapter = new SqlDataAdapter(selectStr, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter); 
adapter.DeleteCommand = builder.GetDeleteCommand(true);
adapter.UpdateCommand = builder.GetUpdateCommand(true);
adapter.InsertCommand = builder.GetInsertCommand(true);
adapter.Fill(employees_table);
Run Code Online (Sandbox Code Playgroud)

我还为表设置了一个主键:

DataColumn[] employees_keys = new DataColumn[2];
employees_keys[0] = employees.Columns["EmployeeID"];
employees_table.PrimaryKey = employees_keys; 
Run Code Online (Sandbox Code Playgroud)

现在我试图删除并添加一行:

// accepts an employee object and creates a new new row with the appropriate values for 
// an employee table row 
DataRow row = ConvertEmployeeToRow(employeeToAdd);
employee_table.Rows.Add(row);`
Run Code Online (Sandbox Code Playgroud)

并删除一行:

DataRow row = employees.Rows.Find(employeeToDismiss.ID);
employees.Rows.Remove(row); 
Run Code Online (Sandbox Code Playgroud)

我还应该指出,我试图使用row.SetAdded()row.Delete() …

c# ado.net

0
推荐指数
1
解决办法
1万
查看次数