使用EF。
处理MVC 5应用程序我的数据库中有3个表。举个简单的例子让我用这个....
汽车和颜色是“主”表。 CarsColors是一个包含各种组合的交叉引用表。
目标:创建一个显示表格的视图。对于每一行,它将显示汽车详细信息。将有一个“颜色”列。在“颜色”列中,将有一个逗号描述的汽车使用的颜色列表。 (这是我问题的核心。)所以,例如....
NSTask *usbCommandTask;
NSPipe *outPipe;
NSPipe *inPipe;
NSPipe *errorPipe;
NSFileHandle *inFile;
NSFileHandle *outFile;
NSTimer *pollTimer;
dispatch_queue_t mtpTask;
@implementation AppDelegate
- (void)commandNotification:(NSNotification *)notification
{
NSData *data = nil;
while ((data = [outFile availableData]) && [data length]){
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
}
NSLog(@"Execution never gets here");
}
-(void)checkTask
{
if(usbCommandTask){
if([usbCommandTask isRunning])NSLog(@"Task running"); else NSLog(@"Task dead");
} else NSLog(@"Task is nil");
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
usbCommandTask = [[NSTask alloc] init];
[usbCommandTask setLaunchPath:@"/Applications/usb-debugger-cli"];
[usbCommandTask setCurrentDirectoryPath:@"/"];
[usbCommandTask setArguments:[NSArray arrayWithObject:@"-i"]];
inPipe = [NSPipe pipe];
[usbCommandTask setStandardInput:inPipe];
outPipe = [NSPipe pipe];
[usbCommandTask setStandardOutput:outPipe];
errorPipe = [NSPipe pipe];
[usbCommandTask setStandardError:errorPipe];
outFile = [outPipe fileHandleForReading];
inFile = [inPipe fileHandleForWriting];
[outFile waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(commandNotification:)
name:NSFileHandleDataAvailableNotification
object:nil];
pollTimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(checkTask) userInfo:nil repeats:TRUE];
[usbCommandTask launch];
NSLog(@"Launched");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
if(usbCommandTask)[usbCommandTask terminate];
}
- (IBAction)clicked:(id)sender {
if(usbCommandTask){
NSString *command=@"help\n";
NSData *commandData=[NSData dataWithBytes:command.UTF8String length:command.length];
[inFile writeData:commandData];
}
else
{NSLog(@"Comamnd task dead");
}
}
我不确定如何做到这一点。我知道我的汽车选择我可以使用.Include()来包含CarsColors。但是,我如何“升级”一个级别才能获得Cars.ColorName?我需要循环通过CarsColors以填充每行的最后一列,但使用Colors表作为实际颜色名称。
由于
答案 0 :(得分:0)
您可以使用导航属性来获取颜色名称:
public partial class CarsColors
{
public int CarId { get; set; }
public int ColorId { get; set; }
public virtual Car Car { get; set; }
public virtual Color Color { get; set; }
}
因此,如果您拥有CarsColors
类型的对象,则可以阅读Color
名称,如下所示:
obj.Color.ColorName
像下面这样的东西可以满足您的需求:
var cars = dbContext.Cars
.Include(car=>car.CarsColors)
.GroupBy(car=>car.CarName)
.Select(gr=> new
{
CarName = gr.Key,
Colors = string.Join(",", gr.Select(car=>car.CarsColors.Color.ColorName))
});