过滤linq中的数据

时间:2018-04-23 13:09:50

标签: c# sql entity-framework linq

目前我使用EF从数据库中检索数据;

我想将4个数据库连接合并为1;

所以我使用以下sql:

select 0 as text
union
select distinct channel        as text    from  CHARGE_ORDER
union
select 1 as text
union      
select distinct  packet_id     as text    from  CHARGE_ORDER
union
select 2 as text
union   
select distinct  recharge_type  as text    from  CHARGE_ORDER
union
select 3 as text
union  
select distinct   order_type    as text    from  CHARGE_ORDER
union
select 4 as text

然后

var total = context.Database.SqlQuery<SelectDto>(totalSql).ToList();

我想像这样使用Linq :(这是错误的)

dto.channelSelect = total.Where(x=>  "0"> x.text.Substring(0, 1) && x.text.Substring(0, 1) < "1").

但是文字是字符串,Linq不起作用;

我可以使用方法来获取我想要的数据

private List<SelectDto> PostHandle(List<SelectDto> list,string start,string end)
            {
                List<SelectDto> result = new List<SelectDto>();
                int len = list.Count;
                for(int i = 0; i < len; i++)
                {
                    if (list[i].text == start)
                    {
                        for(int j = i+1; j < len; j++)
                        {
                            if (list[j].text != end)
                            {
                                result.Add(list[j]);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                }
             return result ;
            }

有没有更好的方法来处理这个要求?

也许我的问题不明确?

所以人们建议我使用skipwhile;但是认为skipWhile无法满足这种需求;

这就是我需要的;我不知道是否有更好的方法

 var total = context.Database.SqlQuery<SelectDto>(totalSql).ToList();
            dto.channelSelect = PostHandleFilter(total, "0", "1");
            dto.packetSelect = PostHandleFilter(total, "1", "2");
            dto.chargeSelect = PostHandleFilter(total, "2", "3");
            dto.orderSelect = PostHandleFilter(total, "3", "4");

0 个答案:

没有答案