开发者

c# dealing with all possible null and non null values

I have the following method:

public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc)
{
    return db.Profiles.Where(p => p.CountryFrom.CountryName.Equals(CountryFrom,
        StringComparison.OrdinalIgnoreCase));
}

What is the best way to write the where clause that would filter all the possible combinations of input parameter开发者_StackOverflow社区s in one statement:

BOTH CountryFrom and CountryLoc = null

Only CountryFrom null

Only CountryLoc null

BOTH CountryFrom and CountryLoc are not null.

Soon .. I would need to filter out profiles by Age, Gender, Profession .. you name it.

I am trying to find a way to write it efficiently in C#. I know how to do it in a clean manner in TSQL. I wish I knew the way. Thanks for all the responses so far.


A good old binary XNOR operation will do the trick here:

db.Profiles.Where(p => !(p.CountryFrom == null ^ p.CountryTo == null))

It's effectively equating two booleans, though to me it's more direct, less convoluted even, than writing ((p.CountryFrom == null) == (p.CountryTo == null))!


I would use this simple LINQ syntax...

BOTH CountryFrom and CountryLoc = null

var result = from db.Profiles select p
             where (p.CountryFrom == null) && (p.CountryLoc == null)
             select p

Only CountryFrom null

var result = from db.Profiles select p
             where (p.CountryFrom == null) && (p.CountryLoc != null)
             select p

Only CountryLoc null

var result = from db.Profiles select p
             where (p.CountryFrom != null) && (p.CountryLoc == null)
             select p

BOTH CountryFrom and CountryLoc are not null.

var result = from db.Profiles select p
             where (p.CountryFrom != null) && (p.CountryLoc != null)
             select p

Hope it helps ;-)


I wouldn't call this elegant:

public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc)
{
    return db.Profiles.Where(p =>
        {
            p.ContryFrom != null &&
            p.CountryFrom.CountryName != null &&
            p.CountryFrom.CountryName.Equals(CountryFrom, StringComparison.OrdinalIgnoreCase)
        });
}


I may be missing something, but as written, your combination of operators will either let all values through or no values through depending on whether you use || or && to combine them together.


I'm in favor of not trying to cram too much logic into a linq expression. Why not contain your comparison logic in a separate function like this?

EDIT: I provided an example implementation of the MatchesCountry function.

class Example
{
    public IQueryable<Profile> FindAllProfiles(string CountryFrom, string CountryLoc)
    {
        return db.Profiles.Where(p => p.MatchesCountry(CountryFrom, CountryLoc));
    }
}

public static class ProfileExtensions
{
    public static bool MatchesCountry(this Profile profile, string CountryFrom, string CountryLoc)
    {
        // NOTE: Your comparison logic goes here.  Below is an example implementation

        // if the CountryFrom parameter was specified and matches the profile's CountryName property
        if(!string.IsNullOrEmpty(CountryFrom) && string.Equals(profile.CountryName, CountryFrom, StringComparison.OrdinalIgnoreCase))
            return true; // then a match is found

        // if the CountryLoc parameter was specified and matches the profile's CountryCode property
        if (!string.IsNullOrEmpty(CountryLoc) && string.Equals(profile.CountryCode, CountryLoc, StringComparison.OrdinalIgnoreCase))
            return true; // then a match is found

        // otherwise, no match was found
        return false;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜