我尝试使用LINQ查询提取数据并添加到模型列表中。在此列表中,我只需要一些字段值,但它返回所有表字段包括空值。我使用模型第一种方法,所以所有模型类都将创建带有所有表字段名称的get set方法,所以我想在检索数据时删除一些字段是否可能?
这是我的代码
for (var z = 0; z < room_ament_list.Count(); z++)
{
var deal_room_amentity_id = room_ament_list[z];
var deal_room_amentity_details = db.deal_room_amentity.Where(a => a.room_amenity_id == deal_room_amentity_id).ToList();
foreach (var item_r_amentity in deal_room_amentity_details)
{
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}
}
这是我的表结构
这是从该列表中返回数据
但是我需要提取我在列表中指定的数据(没有null元素)。是否可以从列表中删除那些空值元素?
答案 0 :(得分:1)
如果我正确理解您的问题,您可以修改LINQ查询,如下所示:
var deal_room_amentity_details = db.deal_room_amentity
.Where(a => a.room_amenity_id == deal_room_amentity_id
&& a.room_amenity_id != null).ToList();
答案 1 :(得分:0)
您可以通过在Linq
查询中指定对象定义,从SQL表中下载所需的信息。
var data = from x in db.deal_room_amentity
where
x.room_amenity_id == deal_room_amentity_id
select new {
id = x.id,
room_amenity_id = x.room_amenity_id
};
foreach(var item_r_amentity in data){
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}