27

Here is my sql query as follow

select enq_Id,enq_FromName,
       enq_EmailId,
       enq_Phone,
       enq_Subject,
       enq_Message,
       enq_EnquiryBy,
       enq_Mode,
       enq_Date,
       ProductId,
       (select top 1 image_name 
        from tblProductImage as i 
        where i.product_id=p.product_Id) as imageName,
       p.product_Name,
       p.product_code    
 from tblEnquiry as e 
 inner join tblProduct as p ON e.ProductId=p.product_Id
 where ProductId is not null 

And I try to convert this sql statement into linq as follow

var result = from e in db.tblEnquiries
             join d in db.tblProducts 
                  on e.ProductId equals d.product_Id                     
             where e.ProductId != null
             orderby e.enq_Date descending
             select new {
                e.enq_Id,
                e.enq_FromName,
                e.enq_EmailId,
                e.enq_Phone,
                e.enq_Subject,
                e.enq_Message,
                e.enq_EnquiryBy,
                e.enq_Mode,
                e.enq_Date,
                d.product_Id,
                d.product_Name,
                imageName = (from soh in db.tblProductImages
                             where soh.product_id == e.ProductId
                             select new { soh.image_name }).Take(1) 
             };

But problem its giving me imageName in a nested list but i want that imageName just as a string .

I also check by using quick watch and in following image you can see that imageName appearing in inner list .

here can be check quick watch result

1 Answer 1

58

Instead of Take(1) which returns sequence IEnumerable<string>, use FirstOrDefault() which returns single string value (or null if there is no results). Also don't create anonymous type for subquery result:

imageName = (from soh in db.tblProductImages
             where soh.product_id == e.ProductId
             select soh.image_name).FirstOrDefault()

BTW FirstOrDefault() generates TOP(1) SQL.

6
  • still its shows in grid view column { image_name = f55c1573_a658_420e_9d33_2a8d997bbd51.jpg } instead of f55c1573_a658_420e_9d33_2a8d997bbd51.jpg . I directly given that result to data source to gridview Apr 4, 2013 at 12:16
  • @rahularyansharma please see small update - that was because you created anonymous type for subquery result: new { soh.image_name }. You should simply return string value. Apr 4, 2013 at 12:18
  • please do one more thing for me is it possible to edit my question title as right now its not look like general and may be some other not get help just by seeing the title . Apr 4, 2013 at 12:29
  • @rahularyansharma sure, no problem. Btw is it Linq to SQL or EF? Apr 4, 2013 at 12:34
  • 2
    @rahularyansharma I believe now question is nicely formatted :) Apr 4, 2013 at 12:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.