简体   繁体   English

选择查询以使用linq根据不同的值获取所有值

[英]select query to get all values based on distinct values using linq

I have n number of services and under each service there will be 2 routes(some times more than 2). 我有n个服务,每个服务下有2条路线(有时超过2条路线)。 Under each route there are n number of stops. 在每个路线下都有n个站点。 I am getting values from db as a table in below column order ServiceId, Service Name,serviceLine color,RouteId, Route Name, Stop Id, Stop Name, Latitude, Longitude. 我从数据库中以表的形式按以下列顺序获取值:ServiceId,Service Name,serviceLine color,RouteId,Route Name,停止ID,停止名称,纬度,经度。

I want to convert it an object list of below format 我想将其转换为以下格式的对象列表

  public class Service { public string ServiceId { get; set; } public string ServiceName { get; set; } public string ServiceLineColor { get; set; } public List<RouteData> RouteList { get; set; } } public class RouteData { public string RouteId { get; set; } public string RouteCode { get; set; } public string RouteName { get; set; } public List<StopData> stopList { get; set; } } public class StopData { public string StopCode { get; set; } public string StopName { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public string StopType { get; set; } } 

Is there any easy way in linq to convert data in to below format? linq中有什么简单的方法可以将数据转换为以下格式? I wanted to avoid looping.. since i am getting nearly 1k records from db. 我想避免循环..因为我从数据库获取了近1k条记录。 Please help me to solve this issue. 请帮我解决这个问题。

Or is it best to use db calls to format data. 还是最好使用db调用来格式化数据。 i didn't prefer that because if there is 50 services i need to do 50 db calls and again have to do data formatting logic. 我不喜欢这样做,因为如果有50个服务,我需要执行50个数据库调用,然后再次必须执行数据格式化逻辑。

To avoid looping over the data structure each time, you could build up additional dictionaries that provide fast access of the objects by id: 为了避免每次都遍历数据结构,您可以建立其他字典,这些字典可通过id提供对对象的快速访问:

var myServiceIndex = new Dictionary<string, Service>()
var myRouteDataIndex = new Dictionary<string, RouteData>()

Service service;
RouteData routData;

foreach (var record in databaseRecords)
{
    if (myRouteDataIndex.TryGetValue(record.RouteId, out route))
    {
        // add stop data
    }
    else if (myServiceIndex.TryGetValue(record.ServiceId, out service)
    {
        // add route data
        // add stop data 
    }
    else
    {
        // add service
        // add route data
        // add stop data 
    }
}

You have a number of stops, and for each stop entry in database you have to map it to a C# object. 您有许多停靠点,对于数据库中的每个停靠点条目,都必须将其映射到C#对象。 In this case, looping is inevitable, as far as I see. 据我所知,在这种情况下,循环是不可避免的。 Linq, and eg. Linq,例如。 entity framework, use looping internally. 实体框架,在内部使用循环。

One option is to use entity framework or Linq to SQL . 一种选择是使用实体框架或Linq to SQL It will give you strong type classes representing each DB table. 它将为您提供代表每个数据库表的强类型类。 But you have to change your DB schema and use foreign keys to link service, route, and stops. 但是您必须更改数据库架构,并使用外键链接服务,路由和停止。

C# code would look exactly like yours, which is auto generated by entity framework and in-sync with DB schema. C#代码看起来与您的代码完全一样,它由实体框架自动生成并与DB模式同步。

Second option is to convert manually . 第二种选择是手动转换 Note, your current schema doesn't complient with Third normal form . 请注意,您当前的架构不符合Third normal form If you don't want to change your schema, you could read and generate them using group by clause. 如果您不想更改架构,则可以使用group by子句读取并生成它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM