Articles Of The Day from ASP.NET website

https://www.asp.net/community/articles

 

Advertisements

The entity or complex type cannot be constructed in a LINQ to Entities query

You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an annonymous type or onto a DTO:

public class ProductDTO
{
    public string Name { get; set; }
    // Other field you may need from the Product entity
}

And your method will return a List of DTO’s.

public List<ProductDTO> GetProducts(int categoryID)
{
    return (from p in db.Products
            where p.CategoryID == categoryID
            select new ProductDTO { Name = p.Name }).ToList();
}

 

Source: https://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query

Another Solution

http://www.c-sharpcorner.com/blogs/error-the-entity-or-complex-type-cannot-be-constructed-in-a-linq-to-entities-query1

 

How do I make EditorFor conditionally readonly?

EDIT: MVC

Controller

ViewBag.Readonly=true;//false

View

@Html.EditorFor(model => model.Quantity, ViewBag.Readonly ? (object)new { htmlAttributes = new { @readonly = "readonly", @class = "form-control" }} : new { htmlAttributes = new { @class = "form-control" } })

Source: https://stackoverflow.com/questions/29153266/how-do-i-make-editorfor-conditionally-readonly

 

 

Asc/Desc tooltip on sorting columns?

 

$(‘th’).each(function(){
if (($(this).hasClass(‘sorting’)) || ($(this).hasClass(‘sorting_desc’))) {
$(this).attr({title: ‘Sort Ascending’});
} else {
$(this).attr({title: ‘Sort Descending’});
}
});

 

Source: https://datatables.net/forums/discussion/718/asc-desc-tooltip-on-sorting-columns

 

ITextSharp – PDF Creation Example

Document document = new Document(PageSize.A4, 0, 0, 50, 50);
System.IO.MemoryStream msReport = new System.IO.MemoryStream();


try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);


    // we add some meta information to the document
    document.AddTitle("My Title");
    document.AddAuthor("Me");
    document.Open();


    for (int i = 1; i <= 5; i++)
    {
        document.NewPage();
        iTextSharp.text.Table datatable = new iTextSharp.text.Table(3);
        datatable.Padding = 2;
        datatable.Spacing = 0;
        float[] headerwidths = { 6, 20, 32 };
        datatable.Widths = headerwidths;
        datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
        datatable.AddCell(i.ToString());
        datatable.AddCell("This is my name.");
        datatable.AddCell("0123456789");


        datatable.AddCell("No");
        datatable.AddCell("Yes");
        datatable.AddCell("No");
     

        document.Add(datatable);
     }
}
catch (Exception e) {
    Console.Error.WriteLine(e.Message);
}


// we close the document 
document.Close();


Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.ContentType = "application/pdf";
Response.BinaryWrite(msReport.ToArray());
Response.End();

 

Source: https://forums.asp.net/t/1428577.aspx?Add+a+new+page+in+iTextSharp