tags:

views:

3230

answers:

2

Hi there, Im trying to instance a Entity called "Produccion" from LINQDataContext in this way :

 Demo.View.Data.PRODUCCION pocoProduccion = (from m in db.MEDICOXPROMOTORs
                                             join a in db.ATENCIONs on m.cmp equals a.cmp
                                             join e in db.EXAMENXATENCIONs on a.numeroatencion equals e.numeroatencion
                                             join c in db.CITAs on e.numerocita equals c.numerocita
                                             where e.codigo == codigoExamenxAtencion
                                             select new Demo.View.Data.PRODUCCION
                                             {
                                                 cmp = a.cmp,
                                                 bonificacion = comi,
                                                 valorventa = precioEstudio,
                                                 codigoestudio = lblCodigoEstudio.Content.ToString(),
                                                 codigopaciente = Convert.ToInt32(lblCodigoPaciente.Content.ToString()),
                                                 codigoproduccion = Convert.ToInt32(lblNroInforme.Content.ToString()),
                                                 codigopromotor = m.codigopromotor,
                                                 fecha = Convert.ToDateTime(DateTime.Today.ToShortDateString()),
                                                 numeroinforme = Convert.ToInt32(lblNroInforme.Content.ToString()),
                                                 revisado = false,
                                                 codigozona = (c.codigozona.Value == null ? Convert.ToInt32(c.codigozona) : 0),
                                                 codigoclinica = Convert.ToInt32(c.codigoclinica),
                                                 codigoclase = e.codigoclase,
                                             }
                                             ).FirstOrDefault();

Im getting the following error :

System.NotSupportedException was caught
  Message="No se permite la construcción explícita del tipo de entidad 'Demo.View.Data.PRODUCCION' en una consulta."
  Source="System.Data.Linq"
  StackTrace:
       en System.Data.Linq.SqlClient.QueryConverter.VisitMemberInit(MemberInitExpression init)
       en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
       en System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
       en System.Data.Linq.SqlClient.QueryConverter.VisitSelect(Expression sequence, LambdaExpression selector)
       en System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
       en System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
       en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
       en System.Data.Linq.SqlClient.QueryConverter.Visit(Expression node)
       en System.Data.Linq.SqlClient.QueryConverter.VisitFirst(Expression sequence, LambdaExpression lambda, Boolean isFirst)
       en System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(MethodCallExpression mc)
       en System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(MethodCallExpression mc)
       en System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
       en System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
       en System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
       en System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
       en System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
       en System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
       en Demo.View.InformeMedico.realizarProduccionInforme(Int32 codigoExamenxAtencion, Double precioEstudio, Int32 comi) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 602
       en Demo.View.InformeMedico.UpdateEstadoEstudio(Int32 codigo, Char state) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 591
       en Demo.View.InformeMedico.btnGuardar_Click(Object sender, RoutedEventArgs e) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 683
  InnerException:

Is that now allowed in LINQ2SQL?

Regards...

+5  A: 

Entities can be created outside of queries and inserted into the data store using a DataContext. You can then retrieve them using queries. However, you can't create entities as part of a query.

Martin Peck
thats means more lines of code right?
Angel Escobedo
How can I pass the select values from that returning query to an Entity, I mean dont using members, just like "varReturningQuery as ProductionEntity"?
Angel Escobedo
+3  A: 

I just ran into the same issue.

I found a very easy solution.

var a = att as Attachment;

Func<Culture, AttachmentCulture> make = 
    c => new AttachmentCulture { Culture = c };

var culs = from c in dc.Cultures
           let ac = c.AttachmentCultures.SingleOrDefault( 
                                           x => x.Attachment == a)
           select ac == null ? make(c) : ac;

return culs;
leppie
select may be better written as select ac ?? make(c);
recursive
Excellent solution - thanks! Any ideas why this isn't allowed without doing this?
Whisk
@Whisk - in case you're still interested, I found http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/1ce25da3-44c6-407d-8395-4c146930004b - '...Constructing entity instances manually as a projection pollutes the cache with potentially malformed objects...'
Alex Humphrey