none
Linq Query with empty string parameter RRS feed

  • Question

  • Hi Folks,

    i´m searching for a query which can handle empty parameters. My Linq-Query looks like:

                    var fileList = (from files in Program.MODEL.tbl_Files
                                where (files.Filename.Contains(SearchText) || files.Filename.Contains("%" + SearchText + "%") || ((DB.tbl_Matchcode)files.tbl_Matchcode).Matchcode.Contains("%" + SearchText + "%"))  && files.FolderID.Equals(FolderID) && files.FileCreator == UserID || files.FileType.Equals(FileType)|| (files.CreationDate.Date >= CreatedOnFrom.Value.Date && files.CreationDate.Date <= CreatedOnTo.Value.Date)
                                select files);

    The thrown exception says:
    Unable to cast the type 'System.Collections.Generic.ICollection`1[[DMS2018.DB.tbl_Matchcode, DMS2018, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' to type 'DMS2018.DB.tbl_Matchcode'. LINQ to Entities only supports casting EDM primitive or enumeration types.

    The Mainproblem is: I try to query my Database by specific parameters even a parameter is empty. I´m pretty sure, that my query is most bad and also most wrong - in fact, i think a "real SQL Query" is more easier than a Linq Query but i don´t want to give up.

    Can anybody help me?

    Thank ya,

    Dom


    C# und VB.NET sind wie eine Schachtel Pralinen, ich weiß nie was ich bekomme xD


    • Edited by Dom2011 Thursday, February 22, 2018 5:49 PM Textcorrection
    Thursday, February 22, 2018 5:44 PM

All replies

  • but i don´t want to give up

    You should.

    Unable to cast the type 'System.Collections.Generic.ICollection`1[[DMS2018.DB.tbl_Matchcode, to type 'DMS2018.DB.tbl_Matchcode'

    LINQ to Entities only supports casting EDM primitive or enumeration types.

    The below are primitive types and enumerations. A Contains() only works with string data, meaning that you would need to be looking at the content of an object's public string property and not looking at a collection.  

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/built-in-types-tablehttps://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/enumeration-types

    To be honest, your Linq statement is so wrong and bad that I don't know what you are trying to do.

    Thursday, February 22, 2018 11:16 PM
  • Hi Dom2011,

    In entity framework, the method named Contains, which will be translate to like method in database, you don't need to add the character "%".

    >>Unable to cast the type 'System.Collections.Generic.ICollection`1[[DMS2018.DB.tbl_Matchcode, DMS2018, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' to type 'DMS2018.DB.tbl_Matchcode'. LINQ to Entities only supports casting EDM primitive or enumeration types.

    If you want to filter child entities, you could use join key to achieve it. like this:

    var fileList = (from files in Program.MODEL.tbl_Files
                join m in Program.MODEL.tbl_Matchcode on files.matchid equals m.matchid
                where (files.Filename.Contains(SearchText) || m.Matchcode.Contains(SearchText))  && files.FolderID.Equals(FolderID) && files.FileCreator == UserID || files.FileType.Equals(FileType)|| (files.CreationDate.Date >= CreatedOnFrom.Value.Date && files.CreationDate.Date <= CreatedOnTo.Value.Date)
                                select files);

    Or like this

    var query = Program.MODEL.tbl_Files.Where(t => t.Filename.Contains(SearchText) || t.tbl_Matchcode.Any(x => x.Matchcode.Contains(SearchText)));

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 23, 2018 5:23 AM
    Moderator