我有两个班级
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp"
android:background="@color/redText"
android:id="@+id/layoutall"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:id="@+id/layoutforBGImage"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="10dp"
android:id="@+id/layoutProfileimage"
android:layout_weight="0.65">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_profile"
android:scaleType="fitCenter"
android:id="@+id/imageforprofile"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/textforprofileLinearLayout"
android:layout_weight="0.25">
<TextView
android:id="@+id/textforprofile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="NAME HERE"
android:freezesText="true"
android:singleLine="true"
android:textColor="@drawable/text_selector"
android:textStyle="bold"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
<TextView
android:id="@+id/textforprofileemail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:text="EMAIL ADDRESS"
android:textColor="@drawable/text_selector"
android:freezesText="true"
android:singleLine="true"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.50"
android:orientation="vertical"
android:id="@+id/layoutforallproduct">
<TextView
android:id="@+id/textformoreproducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="VALUE IN BOTTOM"
android:freezesText="true"
android:singleLine="true"
android:textColor="@drawable/text_selector"
android:textStyle="bold"
tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry" />
</LinearLayout>
</LinearLayout>
和两个数据表ds.Tables [1]和ds.Tables [2]
我正在将数据从两个数据集转换为Item类的列表,该类也包含Itemcode类的列表
我正在做的事情如下
public class Item
{
public string LineID{ get; set; }
public string ItemNo{ get; set; }
public List<Itemcode> ItemcodeList{ get; set; }
}
public class Itemcode
{
public string LineID{ get; set; }
public string code { get; set; }
public string codeValue { get; set; }
}
上面的代码给出了ds.Table [1]
每行的所有ds.Tables [2]行但我只想要那些ds.Table [1]的LineID与ds.Table [2]的LineID匹配的行,我必须改变条件
答案 0 :(得分:3)
var itemCodes = (
from dataRow in ds.Tables[2].AsEnumerable()
select new Itemcode
{
LineID = dataRow.Field<string>("LineID"),
code = dataRow.Field<string>("code"),
codeValue = dataRow.Field<string>("code")
}).ToLookup(ic => ic.LineID);
var lines = (
from dataRow in ds.Tables[1].AsEnumerable()
let lineID = dataRow.Field<string>("LineID")
select new Item
{
LineID = lineID,
ItemNo = dataRow.Field<string>("ItemNo"),
ItemcodeList = itemCodes[lineID].ToList()
}).ToList();
或者,如果您更喜欢方法链语法:
var itemCodes = ds.Tables[2].AsEnumerable().Select(dataRow => new Itemcode
{
LineID = dataRow.Field<string>("LineID"),
code = dataRow.Field<string>("code"),
codeValue = dataRow.Field<string>("code")
}).ToLookup(ic => ic.LineID);
var lines = ds.Tables[1].AsEnumerable()
.Select(dataRow => new {dataRow, lineID = dataRow.Field<string>("LineID")})
.Select(item => new Item
{
LineID = item.lineID,
ItemNo = item.dataRow.Field<string>("ItemNo"),
ItemcodeList = itemCodes[item.lineID].ToList()
}).ToList();
答案 1 :(得分:3)
我认为使用group join将是一个很好的解决方案。
var items= (from tbl1 in ds.Tables[1].AsEnumerable()
join tbl2 in ds.Tables[2].AsEnumerable()
on tbl1.Field<int>("LineId") equals tbl2.Field<int>("LineID") into g
select new Item
{
LineID = tbl1.Field<int>("LineID").ToString(),
ItemNo = tbl1.Field<string>("ItemNo"),
ItemcodeList = g.Select(row =>
new Itemcode
{
code = row.Field<string>("code").ToString(),
codeValue = row.Field<string>("codeValue").ToString(),
}).ToList()
}).ToList();
答案 2 :(得分:0)
只需添加where子句并将其与datarow中的LineId进行比较
List<Item> items = ds.Tables[1].AsEnumerable().Select(dataRow => new Item
{
LineID= Convert.ToString(dataRow.Field<int>("LineID")),
ItemNo= dataRow.Field<string>("ItemNo"),
ItemcodeList = ds.Tables[2].AsEnumerable().Where(ic=>ic.Field<int>("LineId")==dataRow.Field<int>("LineID")).Select(row => new Itemcode
{
code= Convert.ToString(row.Field<string>("code")),
codeValue = Convert.ToString(row.Field<string>("codeValue")),
}).ToList()
}).ToList();
其他信息
这里AsEnumerable与AsQueryable不同....这里的数据已经从您的数据库中获取并填充在DataSet的DataTable中....您必须做的事情AsEnumerable是因为DataTable没有实现 IEnumerable&lt; T&gt; ,您的所有Linq查询仅适用于 IEnumerable&lt; T&gt; 类型。
答案 3 :(得分:0)
如果你想要 lambda 解决方案,下面应该有效:
List<Item> items =
ds.Tables[0].AsEnumerable().GroupJoin(
ds.Tables[1].AsEnumerable(),
tab1 => tab1.Field<int>("LineID"),
tab2 => tab2.Field<int>("LineID"),
(tab1, tab2) => new Item
{
LineID = Convert.ToString(tab1.Field<int>("LineID")),
ItemNo = tab1.Field<string>("ItemNo"),
ItemcodeList = tab2.AsEnumerable().SelectMany(codes=>
new List<Itemcode>
{
new Itemcode { code = codes.Field<string>("Code")}
}
).ToList()
}
).ToList();