开发者

LINQ to Entities How do do a subquery for a field

To save multiple DB calls, and since it is from the same table, I'm looking for one of the fields in my linq query to return an object with 2 fields that are IEnumerable.

开发者_开发百科

I wrote some pseudeocode here that should illustrate what I'm trying to do, but its not valid Linq code. Anyone know how to make this work? (Fred & Joe will both be IEnumerable)

    var c = from jobs in model.jobView
                      select jobs.JobID, jobs.NameID, new
                                 {
                                     Fred = from j in model.jobView
                                            select jobs.Field1,
                                     Joe = from k in model.jobView
                                            select jobs.Field2
                                 };


You want to create an anonymous type and then create another anonymous type within it.

I am guessing there is a typo in your two collections where you use j and k, but select with jobs

 var c = from jobs in model.jobView      
         select new 
         {
           jobs.JobID, 
           jobs.NameID, 
           TwoObjects = new
                        {
                          Fred = from j in model.jobView
                                 select jobs.Field1,
                          Joe = from k in model.jobView
                                 select jobs.Field2
                        }
        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜