Linq Query使用空字符串参数 [英] Linq Query with empty string parameter

查看:92
本文介绍了Linq Query使用空字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Folks,

我正在搜索可以处理空参数的查询。我的Linq-Query看起来像:

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);

抛出的异常说:
$
无法强制转换类型'System.Collections.Generic.ICollection`1 [[DMS2018.DB.tbl_Matchcode,DMS2018,Version = 1.0.0.0,Culture = neutral ,PublicKeyToken = null]]'键入'DMS2018.DB.tbl_Matchcode'。 LINQ to Entities仅支持转换EDM原语或枚举
类型。

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.

主要问题是:我尝试通过特定参数查询我的数据库,即使参数为空。我很确定,我的查询最糟糕也是最错误的 - 实际上,我认为是"真正的SQL查询"。比Linq查询更容易,但我不想让
放弃。

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?

谢谢ya ,

Dom

C#und VB.NET sind wie eine Schachtel Pralinen,ichweißnieis ich bekomme xD

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

推荐答案

但我不想放弃

你应该。

You should.

无法转换类型'System.Collections.Generic.ICollection`1 [[DMS2018.DB.tbl_Matchcode,  键入'DMS2018.DB.tbl_Matchcode'

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

LINQ to Entities仅支持转换EDM原语或枚举类型。

以下是原始类型和枚举。 Contains()仅适用于字符串数据,这意味着您需要查看对象的公共字符串属性的内容而不是查看集合。  

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-table https://docs.microsoft.com/en-us/dotnet/csharp/programming-指南/枚举类型

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

老实说,你的Linq语句是如此错误和糟糕,以至于我不知道你想要做什么。

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


这篇关于Linq Query使用空字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆