小编Nik*_*wal的帖子

DataTable.Copy()或DeepClone.哪一个选择?

我有一个困惑

场景:

我想创建一个DataTable的副本,以添加到另一个DataSet.有两种方法(AFAIK):

1. Make a Copy using DataTable.Copy()
2. Make a Deep Clone using 

public static T DeepClone<T>(this T source)
{
    if (!typeof(T).IsSerializable)
        throw new ArgumentException("The type must be serializable.", "source");

    // Don't serialize a null object, simply return the default for that object
    if (Object.ReferenceEquals(source, null))
        return default(T);

    IFormatter formatter = new BinaryFormatter();
    Stream stream = new MemoryStream();
    using (stream)
    {
        formatter.Serialize(stream, source);
        stream.Seek(0, SeekOrigin.Begin);
        return (T)formatter.Deserialize(stream);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的困惑:

  1. 有没有其他方法可以做到这一点?
  2. 哪一个更好,为什么?
  3. 是否DataTable.Copy()在内部使用DeepClone或一些其他的逻辑是什么?

.net c# datatable clone

6
推荐指数
2
解决办法
4312
查看次数

GetTemplateChild 在 .Net 3.5 中是否已过时,FrameWorkTemplate.FindName 和 ControlTemplate.FindName 之间有什么区别

我覆盖了 ResourceDictionary 中控件的模板Generic.xaml。我添加了一个按钮,我想在上面添加一些事件。

<Setter Property="Template">
    <Setter.Value>
        --Added my button here.
    </Setter.Value>
</Setter>
Run Code Online (Sandbox Code Playgroud)

所以在加载控制事件中我做了

Button  b = (Button)mycontrol.Template.FindName("PARTName", mycontrol)

//Add Events on my button
Run Code Online (Sandbox Code Playgroud)

我在互联网上读到的一些我可以做的地方

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    UIElement editingelement = GetTemplateChild("PART_EditingElement");
    if (editingelement != null) 
    {
        // do something
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试做这个建议时GetTemplateChild说不要使用。

在此处输入图片说明

所以我的问题是

  1. 为什么不使用GetTemplateChild. 它过时了吗?和

  2. FrameWorkTemplate.FindName和 和有ControlTemplate.FindName什么区别?

c# wpf controltemplate

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

SQL合并错误:MERGE语句尝试更新或删除

我有一个源表

select 54371 Id, 'foo' [CreateBy], '2016-10-24 09:29:18.548'[CreateDate], 'foo'[UpdateBy],  '2016-10-24 09:29:18.548'[UpdateDate], 'E'[MT], 185761[MID], 3[BGID]
union
select 54372, 'foo', '2016-10-24 09:30:18.548', 'foo',  '2016-10-24 09:30:18.548',  'E', 185761, 2
Run Code Online (Sandbox Code Playgroud)

和目标表

select 54379 Id, 'foo' [CreateBy], '2016-10-24 09:29:18.548'[CreateDate], 'foo'[UpdateBy],  '2016-10-24 10:29:18.548'[UpdateDate], 'E'[MT], 185761[MID], 3[BGID]
Run Code Online (Sandbox Code Playgroud)

我想要的是基于MT,MID和

  1. 如果不存在则插入
  2. 如果BGID匹配则更新
  3. 如果BGID不匹配则删除

当我使用SQL Merge语句时,我收到错误

MERGE语句尝试多次更新或删除同一行.当目标行与多个源行匹配时会发生这种情况.MERGE语句不能多次更新/删除目标表的同一行.优化ON子句以确保目标行最多匹配一个源行,或使用GROUP BY子句对源行进行分组

我的合并是这样的

MERGE   
  FooBar AS target
USING
(
SELECT
E.[Id],
E.[CreateBy],
E.[CreateDate],
E.[UpdateBy],
E.[UpdateDate],
E.[MT],
E.[MID],
E.[BGID]
FROM @FooBar E
) AS source
ON
source.MID = target.MID
AND source.MT = target.MT
WHEN MATCHED …
Run Code Online (Sandbox Code Playgroud)

t-sql sql-server merge

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

LINQ是否适用于Index?

让我们说我有一个阵列

byte[] myarr = {1,4,3,4,1,2,1,2,4,3,1,4,2};    
Run Code Online (Sandbox Code Playgroud)

myarr的长度为13(0-12索引),也是int [] val的长度.

int[] val = new int[13];
Run Code Online (Sandbox Code Playgroud)

我想检查myarr的索引,其值为4即1,3,8,11.

然后我想要

val[1]++;
val[3]++;
val[8]++;
val[11]++;
Run Code Online (Sandbox Code Playgroud)

一种方法是使用for循环

for(int i=0; i<myarr.length; i++)
{
   if(myarr[i] == 4)
      val[i]++;
}
Run Code Online (Sandbox Code Playgroud)

我们可以使用Array.indexof,但它返回该值的第一个索引,这意味着该值必须是唯一的,而my my有很多相同的值.

可以使用linq完成吗?

c# linq arrays

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

如何创建Between Extension方法

我有一个变量,其值在运行时填充.我想检查该值是否在两个相同的数据类型值(比如最低和最高)之间,或者是否使用扩展方法.

我想检查一下

int a = 2; //here static but is can be changed at runtime

if(a.Between(0,8))
   DoSomething();
else
   DoNothing();
Run Code Online (Sandbox Code Playgroud)

如果a是0或8或它们之间的任何值,它应该返回true.

如果a是(-1或更小)或(9或更大)那么它应该返回false

我想创建一个类似的扩展方法

public static bool Between<T1>(this T1 val1, T1 lowest, T1 highest) where ????
{
    What code to write here???? 
}
Run Code Online (Sandbox Code Playgroud)

c# extension-methods

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

从另一个类C#访问表单的控件

我是c#和visual studio的新手,但不是一般的编程.我在3天内搜索了我的问题的答案,我发现了很多,但是出于一些奇怪的原因(我确定我错过了一些非常明显的东西)我无法让它发挥作用.我认为这是像我这样的新手问题.我有一个表单(Form3),带有一个文本框和一个按钮(我设置它只是为了测试目的).我想从另一个类填充和阅读此文本框.我知道最合适的方法是使用GET和SET访问器在Form3.cs中创建一个属性.我做到了但我无法让它发挥作用.我没有收到任何错误消息,但我也无法设置文本框的值.它只是空白.这是我的示例代码:

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public string setCodes
        {
            get { return test1.Text; }
            set { test1.Text = value; }
        }

        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {   }

        private void button1_Click(object sender, EventArgs e)
        {
            a.b();
        }
    }

    public class a
    {       
        public static void b()
        {
            Form3 v = new Form3();
            v.setCodes = "abc123";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个问题吗?

c#

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

新Foo {A = 1,} Bug或功能?

以下C#片段在我的Visual Studio 2010下编译并运行:

struct Foo {
    public int A;
}

// ..

var foo = new Foo { A = 1, };
Run Code Online (Sandbox Code Playgroud)

请注意对象初始值设定项中的尾随逗号.

这是合法的C#并且它有任何有用的用途,或者我刚刚遇到(良性)编译器错误?

c# c#-4.0

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

用于保存3列并通过任何列快速查找行的结构

我们有一个哈希表作为对这样的值列表的只读引用:

internal static readonly Hashtable relationcodeAcodeB = new Hashtable
{
    {"149", "23"},
    {"139", "17"}
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要一个可以容纳3个值(列)的结构,并通过任何其他2来快速查找值.

像这样的东西:

internal static readonly Hashtable relationcodeAcodeBcodeC = new Hashtable
{
    {"149", "23", "xx"},
    {"139", "17", "xxx"}
}
string codeB=relationcodeAcodeBcodeC[codeA="149"]["codeB"];
Run Code Online (Sandbox Code Playgroud)

c# hashtable data-structures

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

使用"绑定与StaticResource"和在WPF中使用"StaticResource"之间的区别是什么

我创建了一个mycustomItemsPanelApp.Resources

<Application.Resources>
    <ItemsPanelTemplate x:Key="mycustomItemsPanel">
        .... Some code here
    </ItemsPanelTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

并以这种方式将其提供给UIControl

<.... ItemsPanel="{StaticResource mycustomItemsPanel}" />
Run Code Online (Sandbox Code Playgroud)

但我知道这可以作为提供

<.... ItemsPanel="Binding Source={StaticResource mycustomItemsPanel}}" />
Run Code Online (Sandbox Code Playgroud)

这些有什么区别?

.net c# wpf xaml

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

WPF:TreeView 或 TreeListView 标题水平滚动问题

我从这里下载了 TreeListView 。当数据被剪切时,它没有显示水平或垂直滚动​​条。像这样

所以我把它的 Style 改为

<Style TargetType="{x:Type l:TreeListView}">
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
            Value="Auto" />
    <Setter Property="VerticalContentAlignment"
            Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:TreeListView}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <DockPanel>
                        <GridViewHeaderRowPresenter Columns="{StaticResource gvcc}"
                                                    DockPanel.Dock="Top" />
                        <ScrollViewer x:Name="_tv_scrollviewer_"
                                      Background="{TemplateBinding Background}"
                                      CanContentScroll="False"
                                      Focusable="True"
                                      HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                      Padding="{TemplateBinding Padding}"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                      VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
                            <ItemsPresenter />
                        </ScrollViewer>
                    </DockPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="VirtualizingStackPanel.IsVirtualizing"
                             Value="true">
                        <Setter Property="CanContentScroll"
                                TargetName="_tv_scrollviewer_"
                                Value="true" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

垂直滚动条很好。但问题是水平 Scollbar。当数据被水平裁剪,滚动条向右移动时,数据向右移动,但标题保持原位。像这样。

如何解决水平滚动 treeitem 时标题随之移动的问题。我不允许在滚动查看器中放置标题,因为在垂直滚动数据时它们需要可见。

c# vb.net wpf treeview treelistview

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