English 中文(简体)
LINQ - 强制字段, 不论是否无效
原标题:LINQ - mandatory field and dont care if null
  • 时间:2012-05-24 16:31:20
  •  标签:
  • c#
  • .net
  • linq

我在一个 asp.net 应用程序中建立一个搜索功能, 我正在使用 LINQ 到 SQL 来检索基于选定搜索标准的数据。 搜索标准是

  1. Country
  2. City
  3. District
  4. number of rooms
  5. Rent Cycle

只有第一个搜索标准,即“国家”,是强制性字段。但是,如果用户输入标准2、3、4和/或5的值,则输入的值应该考虑在内,只检索与所有输入的搜索标准匹配的结果。请注意,如果标准2、3、4和/或5中的一个是空的(null),LINQ应该作为DONT CARE, 并返回该行。

例如,如果所输入的标准是:

  1. Country = USA
  2. City = null
  3. District = null
  4. number of rooms = null
  5. Rent Cycle = null

然后所有与国家分行 = 美国应该返回。

另一个例子:

  1. Country = UK
  2. City = null
  3. District = null
  4. number of rooms = 5
  5. Rent Cycle = null

然后,所有与国家行 = = 英国和数字房间 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

我怎样才能在LINQ到SQL实现这个目标?

以下是我到目前为止所知道的:

var data = from x in db.Units where x.Country == coutnryID && /*PLEASE HELP!*/ select x;
最佳回答

试一下(假设 cityId , reaID , room rentCycle 是您想要搜索的变量:

var data = from x in db.Units
    where x.Country == countryId
        && (cityId == null || x.City == cityId)
        && (districtId == null || x.District == districtId)
        && (rooms == null || x.Rooms == rooms)
        && (rentCycle == null || x.RentCycle == rentCycle)
    select x; 

我基本上是说,如果您想要搜索的变量是无效的,那么不考虑这些变量,否则将它们与 United 中的相应字段匹配。

问题回答

您可以分阶段建立查询 :

var query = from x in db.Units  where x.Country == countryId;
if (cityId != null)        query = query.Where(x.City == cityId);
if (districtId != null)    query = query.Where(x.City == districtId);
if (rooms != null)         query = query.Where(x.Rooms == rooms);
if (rentCycle != null)     query = query.Where(x.RentCycle == rentCycle);
var data = query.Select();

如果小菜一碟的话 SQL就更有效率了

GetValue OrDefault 使用 GetValue OrDefault, 如果为空, 则向当前值提供默认值 :

var data = from x in db.Units
    where x.Country == countryId
        && (x.City == cityId.GetValueOrDefault(x.City))
        && (x.District == districtId.GetValueOrDefault(x.District))
        && (x.Rooms == rooms.GetValueOrDefault(x.Rooms))
        && (x.RentCycle == rentCycle.GetValueOrDefault(x.RentCycle))
    select x; 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签