Select with Linq set the first characters in Where

Asked

Viewed 141 times

1

How can I define in Linq the same that I define in clausular Where do sql server in the form below ..

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

In other words, I’m just getting what starts with what I’m going through, I’m using the Contains, but so it checks whether what I’m comparing contains in string whether it is at the beginning or at 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

inserir a descrição da imagem aqui

I just want what starts with 1.4.1

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'

Follow 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

1

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

Stayed like this

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

Source here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.