小编jzm*_*jzm的帖子

C#:将一个部分类转换为父类并将其放入一个Dictionary中

说我有以下课程:

public class Parent
{
   public Property1 {get;set;}
   public Property2 {get;set;}
}

public partial class Child : Parent { }

public partial class Cousin : Parent { }
Run Code Online (Sandbox Code Playgroud)

我有返回IQueryable<Child>IQueryable<Cousin>使用Property1和Property2的方法.

是否可以将两个列表添加到一个Dictionary<string, List<Parent>>

我试过了:

var childList = db.GetChildList() as List<Parent>;
Run Code Online (Sandbox Code Playgroud)

但是这会返回null.

List<Child>在将它添加到字典之前,我是否必须返回并将它们添加到List中?

谢谢

c# dictionary list

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

使用Linq展平/合并列表

假设我有一个XML文件:

<locations>
    <country name="Australia">
        <city>Brisbane</city>
        <city>Melbourne</city>
        <city>Sydney</city>
    </country>
    <country name="England">
        <city>Bristol</city>
        <city>London</city>
    </country>
    <country name="America">
        <city>New York</city>
        <city>Washington</city>
    </country>
</locations>
Run Code Online (Sandbox Code Playgroud)

我希望它被夷为平地(这应该是最终的结果):

Australia
Brisbane
Melbourne
Sydney
England
Bristol
London
America
New York
Washington
Run Code Online (Sandbox Code Playgroud)

我试过这个:

var query = XDocument.Load(@"test.xml").Descendants("country")
    .Select(s => new
    {
        Country = (string)s.Attribute("name"),
        Cities = s.Elements("city")
            .Select (x => new { City = (string)x })
    });
Run Code Online (Sandbox Code Playgroud)

但是这会返回一个嵌套列表query.像这样:

{ Australia, Cities { Brisbane, Melbourne, Sydney }},
{ England, Cities { Bristol, London }},
{ America, Cities { New …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-xml

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

阅读http.Response Body流

我正在发送http请求并以text/xml格式获取响应.我不需要解析XML,只是尝试将其作为字符串返回进行测试.但它似乎很慢.

XML响应是278kb,但它可能需要大约1.7,但有时只需要达到4.5秒才能读取响应体.我可以一遍又一遍地重新运行它,它可以变化很大.我不确定是什么导致它,因为我是Go的新手.

我觉得ioutil.ReadAll()函数让我感到失望.但我也尝试过bufio.NewReader和reader.ReadBytes.一切似乎都很慢.

这就是我的功能:

client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(requestBody))
if err != nil {
    fmt.Println("New Request Error", err)
}

// Set Headers
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Encoding", "gzip")
req.Header.Set("Accept-Encoding", "gzip, deflate")

defer req.Body.Close()

// Get Response
startRes := time.Now()
response, err := client.Do(req)
if response != nil {
    defer response.Body.Close()
}
if err != nil {
    fmt.Println("POST Error", err)
}
endRes := time.Now()
fmt.Println("Response took", endRes.Sub(startRes))

// Read Response Body
startRead := time.Now()
body, readErr := ioutil.ReadAll(response.Body)
if …
Run Code Online (Sandbox Code Playgroud)

go

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

在实例化类时,属性是否实例化?

我想知道当它的容器类是以下时是否创建了属性get {}:

例如

public class MyClass
{
    public MyProperty1 { get { return new MyClass1("Whatever"); }
    public MyProperty2 { get { return new MyClass2("Whatever"); }
    public MyProperty3 { get { return new MyClass3("Whatever"); }
    // so on
}
Run Code Online (Sandbox Code Playgroud)

所以当我这样做时:

var myClass = new MyClass();
Run Code Online (Sandbox Code Playgroud)

这会初始化所有属性(即新的MyClass1,新的MyClass2等),还是只在我这样访问它们时才创建/初始化它们:

myClass.MyProperty1.SomeMethodInsideMyClass1();
Run Code Online (Sandbox Code Playgroud)

在这个例子中,让我们说这个调用的方法,只需要MyClass1方法,但不需要其余的,这就是为什么我想知道这是否会导致任何不必要的开销.

c#

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

标签 统计

c# ×3

dictionary ×1

go ×1

linq ×1

linq-to-xml ×1

list ×1