问题描述
不确定如何为这个问题写一个好标题...:)
我是Linq的新手,不确定该问题要使用什么语法,我有
class MainClass() { string MainKey {get;set;} string MainName {get;set;} List<SmallObject> MainList {get;set} } class SmallObject() { string SmallKey {get;set} }
linq:
mytable包含4个字符串字段(field1,field2,field3,field4)
var mainQuery = (from v from myTable select v) var myQuery = (from v in mainQuery select new MainClass() { MainKey = v.Field1, MainName = v.Field2, MainList = #whats the correct syntax here?# })
这里标有正确语法的零件?#我的问题是我的问题. 我想将v.field3和v.field4作为项目添加到Mainclass中的主要列表 - 对象,但我不知道该怎么做.
我很高兴有人能提供帮助.
推荐答案
您可以创建List<T>(或任何其他集合),并使用收集初始化器:
MainList = new List<SmallObject> { v.Field3, v.Field4 }
更新:
由于Field3和Field4是字符串,您只需要做您已经做的事情就将它们放入SmallObject s:
MainList = new List<SmallObject> { new SmallObject { SmallKey = v.Field3 }, new SmallObject { SmallKey = v.Field4 }, }
或,您可以添加一个SmallObject构造函数,该构建器使用字符串参数并像new SmallObject(v.Field3).
一样使用它其他推荐答案
不确定我是否正确理解您.但是以下对我有用:
var myTable = new [] { new {Field1 = "a", Field2 = "b", Field3 = "c", Field4 = "d"}, new {Field1 = "a", Field2 = "b", Field3 = "c", Field4 = "d"} }; var myQuery = (from v in myTable select new MainClass() { MainKey = v.Field1, MainName = v.Field2, MainList = new List<SmallObject>{ new SmallObject {SmallKey = v.Field3}, new SmallObject {SmallKey = v.Field4}, } });
myquery充满了2个对象(主级),两个对象都有一个主列表,每个对象都包含2个项目(SmallObject).
顺便说一句,没有真正看到以下行是:
var mainQuery = (from v from myTable select v)
,但是正如我所说的,也许我不正确理解你
gl!
问题描述
Wasn't sure how to write a good title for this question... :)
I'm new to Linq and not sure what syntax to use for this problem I have
class MainClass() { string MainKey {get;set;} string MainName {get;set;} List<SmallObject> MainList {get;set} } class SmallObject() { string SmallKey {get;set} }
Linq:
myTable contains of 4 string fields (Field1, Field2, Field3, Field4)
var mainQuery = (from v from myTable select v) var myQuery = (from v in mainQuery select new MainClass() { MainKey = v.Field1, MainName = v.Field2, MainList = #whats the correct syntax here?# })
The part marked with #whats the correct syntax here?# is my problem. I want to add v.Field3 and v.Field4 as items to the MainList-object in MainClass, but I don't know how to do that.
I'm happy if anyone can help.
推荐答案
You can create a List<T> (or any other collection) and add items to it with a collection initializer:
MainList = new List<SmallObject> { v.Field3, v.Field4 }
Update:
Since Field3 and Field4 are strings, you would just do what you already do to make them into SmallObjects:
MainList = new List<SmallObject> { new SmallObject { SmallKey = v.Field3 }, new SmallObject { SmallKey = v.Field4 }, }
Or, you could add a SmallObject constructor that takes a string argument and use it like new SmallObject(v.Field3).
其他推荐答案
Not really sure if I understand you correctly. But the following works for me:
var myTable = new [] { new {Field1 = "a", Field2 = "b", Field3 = "c", Field4 = "d"}, new {Field1 = "a", Field2 = "b", Field3 = "c", Field4 = "d"} }; var myQuery = (from v in myTable select new MainClass() { MainKey = v.Field1, MainName = v.Field2, MainList = new List<SmallObject>{ new SmallObject {SmallKey = v.Field3}, new SmallObject {SmallKey = v.Field4}, } });
myQuery is filled with 2 objects (MainClass), with both a Mainlist, each containting 2 items (SmallObject).
Btw, don't really see what the following line is for:
var mainQuery = (from v from myTable select v)
But as I said, maybe I don't understand you correctly
GL!