Select with Linq set the first characters in the where

1

How can I set in Linq the same as I define in the Where do sql server clause of the form below ..

select * from tb_CentrosCusto cc
where cc.Clasificacao like '1.4.1%'

In other words, I'm trying to find out what's going on with the Contains , whether it's at the beginning or the end.

List<Int32> Ids = new List<Int32>();
foreach (var ClasificacaoPai in CCPais)
{
    var IdCentroCusto = CentrosCusto
         .Where(CC => CC.Clasificacao.Contains(ClasificacaoPai))
         .Select(CC => CC.IdCentroCusto)
         .ToList();

    if (IdCentroCusto.Count > 0)
        Ids.AddRange(IdCentroCusto);
}

See how it comes

I just want to get what starts with 1.4.1

    
asked by anonymous 13.11.2015 / 15:14

2 answers

2

Below some forms of "Like".

string.Contains("pattern") is equivalent to LIKE '%pattern%'
string.StartsWith("pattern") is equivalent to LIKE 'pattern%'
string.EndsWith("pattern") is equivalent to LIKE '%pattern'

Here's an example.

List<BaseClaim> BaseClaims = new List<BaseClaim>()
    {
        new BaseClaim(){ WPId = "11123411" }, //match 1
        new BaseClaim(){ WPId = "11123123" }, //match 2
        new BaseClaim(){ WPId = "44423411" }, //match 3
        new BaseClaim(){ WPId = "444AAAA" }, //match 3
        new BaseClaim(){ WPId = "444BBBB" }, //match 3
        new BaseClaim(){ WPId = "444QWQEQW" }, //match 3
        new BaseClaim(){ WPId = "2314" },
        new BaseClaim(){ WPId = "3214" }
    };
    List<UserProfile> UserProfiles = new List<UserProfile>()
    { 
        new UserProfile(){ WPId="%112341%", ID="1459" }, //match 1
        new UserProfile(){ WPId="%123", ID="1459" }, //match 2
        new UserProfile(){ WPId="444%", ID="1459" }, //match 3
        new UserProfile(){ WPId="5555", ID="1459" },
        new UserProfile(){ WPId="2222", ID="1459" },
        new UserProfile(){ WPId="1111", ID="4444" },
    };

    char[] asterisk = { '%' };
    List<BaseClaim> result = BaseClaims.Where(b => UserProfiles.Where(u => u.ID == "1459").Any(u => u.WPId.StartsWith("%") && u.WPId.EndsWith("%") ? b.WPId.Contains(u.WPId.Trim(asterisk)) :
                                                                                                    u.WPId.StartsWith("%") ? b.WPId.EndsWith(u.WPId.Trim(asterisk)) : 
                                                                                                    u.WPId.EndsWith("%") ? b.WPId.StartsWith(u.WPId.Trim(asterisk)) :
                                                                                                     false)).ToList();
    //this will result to getting the first 3 BaseClaims
    
13.11.2015 / 15:24
1

The way to do this was found with the method StartsWith of class String

It was like this

.Where(CC => CC.Clasificacao.StartsWith(ClasificacaoPai))

Source Here

    
13.11.2015 / 15:39