小编Arp*_*wal的帖子

RemoveAll for ObservableCollections?

我正在寻找Linq方式(如List的RemoveAll方法),它可以从我的ObservableCollection中删除所选项目.

我太新了,无法为自己创建扩展方法.有没有办法从ObservableCollection中删除传递Lambda表达式的项目?

c# observablecollection removeall

68
推荐指数
4
解决办法
4万
查看次数

是否可以使用循环引用执行序列化?

因此,我的实体类(用C#编写)遵循父子模型,其中每个子对象必须具有Parent属性,在该属性中它保持对其Parent的引用.

由于循环引用,此Parent属性导致Object序列化的问题.

我无法删除对父级的引用,也不能将其标记为XmlIgnore(因为我在反序列化XML时需要将其读回)

有什么想法吗?

c# serialization circular-reference

14
推荐指数
2
解决办法
7731
查看次数

在Sql中从XML中提取数据的SUM

我在这样的SQL表中有一个XML字段

<Root>
 <Name>Apollo</Name>
 <Code>1000</Code>
 <Code>2000</Code>
 <Code>3000</Code>
</Root>
Run Code Online (Sandbox Code Playgroud)

我需要编写一个SQL查询来选择'Name'和'Code'值的SUM

SELECT 
 T1.c.value('Name[1] AS VARCHAR(100)') AS Name,
 T1.c.value('Code[1] AS NUMERIC(10,5)') AS TotalCode
FROM TableName
CROSS APPLY xmlField.nodes('Root') AS T1(c)
Run Code Online (Sandbox Code Playgroud)

它给了我这样的输出:

Name                Code
---------------------------
Apollo              1000
Apollo              2000
Apollo              3000
Run Code Online (Sandbox Code Playgroud)

但我需要所有Code标签的值的SUM,如下所示:

Name                Code
---------------------------
Apollo              6000
Run Code Online (Sandbox Code Playgroud)

任何想法如何获得标签值的总和?

xml sql field

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

运行时如何知道盒装值类型的确切类型?

我明白拳击是什么.值类型被装箱到对象/引用类型,然后作为对象存储在托管堆上.但是我不能通过拆箱来解决问题.

取消装箱会将对象/引用类型转换回值类型

int i = 123;          // A value type
object box = i;       // Boxing
int j = (int)box;     // Unboxing
Run Code Online (Sandbox Code Playgroud)

好的.但是,如果我尝试将值类型拆分为另一个值类型,例如,在上面的示例中,它会抛出InvalidCastException

long d = (long)box;
Run Code Online (Sandbox Code Playgroud)

它让我有一个想法,可能是运行时隐式知道"box"对象中框内的实际TYPE值类型.如果我是对的,我想知道这种类型信息的存储位置.

编辑:

因为int可以隐式转换为long.这让我很困惑.

int i = 123;
long lng = i;
Run Code Online (Sandbox Code Playgroud)

非常好,因为它没有涉及拳击/拆箱.

c# boxing unboxing

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

如何手动处理非托管资源?

我正在使用一些非托管代码,如 -

 [DllImport("wininet.dll")]
    private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
    //Creating a function that uses the API function...
    public static bool IsConnectedToInternet() {
        int Desc;
        return InternetGetConnectedState(out Desc, 0);
    }
Run Code Online (Sandbox Code Playgroud)

关于在调用Dispose时如何处理/清理这个extern静态对象的任何建议?

c# unmanagedresources

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

水平排列ListView项目

我正在使用ListView,它在数据源的一个属性(资源)上分组.我的要求是显示每个组与其他组水平对齐,但我的实现(如下所示)显示了veritcally对齐的组

            <ListView x:Name="listViewResourceHours" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Width="300" >
            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                <ControlTemplate TargetType="GroupItem">
                                    <StackPanel Orientation="Horizontal">
                                        <ContentPresenter/>
                                        <ItemsPresenter/>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListView.GroupStyle>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel> 
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
                        <Label VerticalAlignment="Center"  Margin="0" Content="{Binding Hours}" />
                        <Label VerticalAlignment="Center"  Margin="2,0,0,0" Content="{Binding WorkingHoursType, Converter={StaticResource ResourceKey=hoursTypeConverter}}" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
Run Code Online (Sandbox Code Playgroud)

以下是此代码结果的示例:

PSE: 0 (B) 0 (NB)
PSC: 0 (B) 0 (NB)
PM: …
Run Code Online (Sandbox Code Playgroud)

wpf listview

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