如何使用linq维护单个列表而不是多个列表?

时间:2019-06-07 12:38:19

标签: c# entity-framework linq

我必须订购列表值。第一个值必须保持不变。其余值应按降序排列。

例如:

list <>信息库= {“汽车”,“自行车”,“飞机”,“火车”,“轮船”};

订购后例外结果列表为:

repository = {“汽车”,“火车”,“轮船”,“自行车”,“飞机”};

如何简化代码?不使用两个以上的列表变量。

       List<Automobile> vehicle = new List<Automobile >();
       List<Automobile> vehicle1 = new List<Automobile>();
       List<Automobile > vehicle2 = new List<Automobile>();

            using (var entity = new AutomobileEntity())
            {
              vehicle = (from details in entity.dbVechiles.Where(i => i.IsActive == true)
                         join sp in entity.dbSpeed.Where(i => i.IsActive) on details.vspeed equals sp.vspeed
                         select new Automobile
                         {
                           Vid = details.vId,
                           VName = details.vName,
                           Vspeed = sp.vspeed,
                           VRate = sp.vrate
                         }).ToList();
            }

  vehicle1 = vehicle.Where(x => x. VName == "BMW").OrderByDescending(x => x. Vid).ToList();
  vehicle2 = vehicle.Where(x => x. VName != "BMW").OrderByDescending(x => x. Vid).ToList();

  vehicle1.AddRange(vehicle2);

  return vehicle1;

6 个答案:

答案 0 :(得分:1)

您可以替换:

vehicle.Where(x => x. VName == "BMW").OrderByDescending(x => x. Vid).ToList();
vehicle.Where(x => x. VName != "BMW").OrderByDescending(x => x. Vid).ToList();

具有:

vehicle.OrderBy(x => x.VName == "BMW").ThenByDescending(x => x.Vid).ToList();

答案 1 :(得分:1)

List<string> repository = new List<string>(new[] { "car", "bike", "airplane", "train", "ship" });
var result = repository.Take(1).Union(repository.Skip(1).OrderByDescending(x => x));

返回

{“汽车”,“火车”,“轮船”,“自行车”,“飞机”}

因此您可以使用

因此,在您的情况下,请使用return

vehicle.Take(1).Union(vehicle.Skip(1).OrderByDescending(x => x.Vid));

如马克所提到的,如果您想防止重复项被删除,您可以合并列表的其余部分,也可以按照他的建议在第一项之前添加。

var result = repository.Take(1).Concat(repository.Skip(1).OrderByDescending(x => x));

答案 2 :(得分:1)

我不确定您到底想要什么,但是这是一种订购列表的方法,但是将第一个保留在其中:

List<string> repository = new List<string>{ "car","bike","airplane","train","ship"};
repository = repository.Skip(1).OrderBy(value => value).Prepend(repository.First()).ToList();

编辑: 刚刚看到@先生。 Belis使用类似的解决方案要快一些,但是如果您使用“ .Union”功能,则必须注意,这也会在列表中引起区别。因此,您列表中的重复项将被删除。

答案 3 :(得分:0)

这种方式:

using (var entity = new AutomobileEntity())
{
    return (from details in entity.dbVechiles.Where(i => i.IsActive == true)
               join sp in entity.dbSpeed.Where(i => i.IsActive) on details.vspeed equals sp.vspeed
               select new Automobile
               {
                   Vid = details.vId,
                   VName = details.vName,
                   Vspeed = sp.vspeed,
                   VRate = sp.vrate
               }).OrderBy(x => x.VName).ThenByDescending(x => x.Vid).ToList();
}

答案 4 :(得分:0)

尽管我不确定要使用LINQ关键字,但这可以在单个查询中完成。

var list = new List<string> {"car", "bike", "airplane", "train", "ship"};

list = (from x in list
      let firstItem = list.First()
      orderby x descending
      orderby x != firstItem
      select x).ToList();

答案 5 :(得分:0)

您可以为获得相同结果而选择的另一种方法。

@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClienteQueryRepositoryImpl implements ClienteQueryRepository {

    private final @NonNull
    JdbcTemplate jdbc;

    @Override
    public List<Cliente> findAll() {
        return jdbc.query(ClienteQuerySQL.SELECT_ALL_CLIENTE_SQL, new ClienteRowMapper());
    }

}
相关问题