4

I have been using entity framework quite some and I can do complex model binding with entity itself easily, but when it comes to raw sql binding I cant find any solutions that is doing complex binding of relation db.

for example following is a entity to model code for product class-

var prodResult = db.products.Where(p => p.isActive == true).Select(p => new ComplexProductModel
                {
                    id = p.id,
                    dateCreated = p.date_created,
                    datePublished = null,
                    desc = p.product_desc,
                    images = db.products_images.Where(pi => pi.prod_id == p.id).Select(pi => pi.prod_img_thumb).ToList(),
                    summary = p.product_summary,
                    title = p.product_name + " " + p.product_code
                }).ToList();

This realizes data against complex Model holding a list type of string or class inside main Complex Model as per query requirements.

Now I tried running same generating query with raw sql execution, but the binding for images object is failing.

var rawComplexData = db.Database.SqlQuery<ComplexProductModel>(dynamicSearchQuery).ToList(); 

Is there any solution for this situation, what I am getting is 10 rows of data(for this example) with images null which should be 7 rows with duplicate fetched images going into List<string> images for the ComplexProductModel.

Here is the complex product model for better understanding the code:

public ComplexProductModel {
        long? id { get; set; }
        string title { get; set; }
        string desc { get; set; }
        string summary { get; set; }
        List<string> images { get; set; }
        DateTime? datePublished { get; set; }
        DateTime? dateCreated { get; set; }
    }

As per @tchelidze request here is dynamicQuery which is basically what i grabbed from what entity framework was generating itself :

SELECT 
    [Project2].[C1] AS [C1], 
    [Project2].[id] AS [id], 
    [Project2].[date_created] AS [date_created], 
    [Project2].[C2] AS [C2], 
    [Project2].[product_desc] AS [product_desc], 
    [Project2].[product_summary] AS [product_summary], 
    [Project2].[C3] AS [C3], 
    [Project2].[C4] AS [C4], 
    [Project2].[prod_img_thumb] AS [prod_img_thumb]
    FROM ( SELECT 
        [Extent1].[id] AS [id], 
        [Extent1].[product_desc] AS [product_desc], 
        [Extent1].[product_summary] AS [product_summary], 
        [Extent1].[date_created] AS [date_created], 
        1 AS [C1], 
        CAST(NULL AS datetime2) AS [C2], 
        [Project1].[prod_img_thumb] AS [prod_img_thumb], 
        CASE WHEN ([Extent1].[product_name] IS NULL) THEN N'' ELSE [Extent1].[product_name] END + N' ' + CASE WHEN ([Extent1].[product_code] IS NULL) THEN N'' ELSE [Extent1].[product_code] END AS [C3], 
        [Project1].[C1] AS [C4]
        FROM  [dbo].[products] AS [Extent1]
        LEFT OUTER JOIN  (SELECT 
            [Extent2].[prod_id] AS [prod_id], 
            [Extent2].[prod_img_thumb] AS [prod_img_thumb], 
            1 AS [C1]
            FROM [dbo].[products_images] AS [Extent2] ) AS [Project1] ON [Project1].[prod_id] = [Extent1].[id]
        WHERE 1 = [Extent1].[isActive]
    )  AS [Project2]
    ORDER BY [Project2].[id] ASC, [Project2].[C4] ASC
5
  • post a dynamicSearchQuery too.
    – tchelidze
    Commented Mar 1, 2018 at 6:51
  • ok putting that in edit
    – Alok
    Commented Mar 1, 2018 at 6:52
  • why do a -1 on this question?
    – Alok
    Commented Mar 1, 2018 at 7:38
  • @tchelidze i have edited the question with more info
    – Alok
    Commented Mar 1, 2018 at 7:43
  • Why? :-) (as asked below, but here for completeness)
    – thmshd
    Commented Mar 1, 2018 at 11:11

1 Answer 1

1

You could introduce a new Object, e.g. let's call it ComplexProductDto and it should match exactly to the query result structure as it is returned from SQL, example:

// Please adjust...
public class ComplexProductDto
{
    public int Id { get; set; }
    public int ProductDesc { get; set; }
    ...
    public string ProdImgThumb { get; set; }
}

Now that data is going to be flattened and ungrouped, containing duplicate records, which you could group in memory using Linq.

var rawComplexData = db.Database.SqlQuery<ComplexProductDto>(dynamicSearchQuery).ToList(); 
var prodResult = rawComplexData
  .GroupBy(x => new
  {
      x.Id,
      x.ProductDesc,
      // List all but Image...
  })
  .Select(x => new ComplexProductModel
  {
      id = x.Key.Id,
      desc = x,Key.ProductDesc,
      .... // etc.
      images = x.Select(i => i.ProdImgThumb).ToList()
  });

The code might be not 100% correct but you get the point. The technique is to retrieve ungrouped data and group it back to desired structure.

4
  • i was thinking about the same, but i truly wanted to avoid another step if entity can do it itself. i mean the in-built function does it. any ways i will wait for alternates and if there are no better solutions i will go with your answer.
    – Alok
    Commented Mar 1, 2018 at 10:38
  • also this begs another issue in case Model needs to be generic then what?
    – Alok
    Commented Mar 1, 2018 at 10:52
  • 1) db is a specific data context, it has the knowledge of all entites, relationships etc., but by using db.Database you basically leave that level and enter raw access. You basically operate outside the Entity Framework DB Context. 2) The "unflattening" technique is specific to objects used, a generic solution is not what you want to build, you don't want to reinvent the Entity Framework :-) Maybe, the source for a better solution lies within the why, what is the purpose of bypassing the EF built in functions? Else, this is going to be a more then bumpy road to continue with.
    – thmshd
    Commented Mar 1, 2018 at 11:05
  • ouch that will be really bumpy road :P
    – Alok
    Commented Mar 1, 2018 at 11:10

Your Answer

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

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