Search string array in collection using LINQ

LINQ behavior is that LINQ wouldn’t return null when results are empty rather it will return an empty enumerable. We can check this with .Any() method;

if (!YourResult.Any())

This is a LinqPad example;

var lst = new List<int>() { 1, 2, 3 };
var ans = lst.Where( i => i > 3 );

(ans == null).Dump();  // False
(ans.Count() == 0 ).Dump();  // True

Let’s go through another example where I have this string array to search;
{“dog”,”cat”};

in this string;
“This is a string and may or may not contain a word we are looking for like cat”

string input = "This is a string and may or may not contain a word we are looking for like cat";
List<string> search = new List<string>() { "dog", "cat"};
bool found = input.Split(' ').Any(x => search.Contains(x));

It works like this: the string gets split into an array of words. Then Any checks whether there is an x in this array where search.Contains(x).

Enumerable.Any(TSource) Method (IEnumerable(TSource)) (System.Linq)

Reference

What does linq return when the results are empty

Find all items in list which exist in another list using linq

Hide/Show Div with javascript

To display and hide DIV in html;

<div class="card-body">
   <div id="divMessage">                
        @Html.Raw(@TempData["message"]);
    </div>
    </div>  
    <div> 
       <!--need to hide this form-->         
       <form method="post" asp-antiforgery="true" id="formDiv">
          <div class="card mb-3">
             <h5 class="card-header text-white">
                  Welcome To Div Hide/Display
             </h5>
        </form>
    </div>
</div>

Use this javascript;

@section Scripts
{
  <script>
     $(document).ready(function () {
            //show/hide login sections based on SSO
            divFormSection();
        });

        function divFormSection() {
            var isDivFormVisible = '@TempData["IsDivFormVisible"]';
            if (isDivFormVisible == 'false') {
                //alert(isDivFormVisible);
                $("#formDiv").hide();
            }
        }
     }
    </script>
}

We need to pass parameters from controller in TempData;

TempData["message"] = "Form Hide/Display demo";
TempData["IsDivFormVisible"] = "false";

Use TempData or ViewBag to render HTML

This is a very basic example;

Declare this in a controller.

var fontAwesomeIcon = "<span class=\"fa fa-redo\" style=\"font-size:30px; color: red; margin-bottom: 20px; \">&nbsp;Try again</span>";

TempData["message"] = $"{fontAwesomeIcon} <h5>Something went wrong. Please try again. If problem persist, reach out to your point of contact for additional information</h5>";

And you can use it in your view;

<div class="card-body">
  <div id="divMessage">
     @Html.Raw(@TempData["message"]);
  </div>
</div>            

Return values from HttpContext.Current.User.Principal and WindowsIdentity.GetCurrent()

Here is a brief explanation;

According to this forum on WindowsIdentity.GetCurrent().Name vs. User.Identity.Name:

  • User.Identity.Name represents the identity passed from IIS.
  • WindowsIdentity.GetCurrent().Name is the identity under which the thread is running.

Depending on your app’s authentication settings in IIS, they will return different values:

AnonymousImpersonateUser.Identity.NameWindowsIndentiy.GetCurrent()
YesTrueEmpty StringIUSR_<machineName>
YesFalseEmpty StringNT Authority\Network Service
NoTruedomain\userdomain\user
NoFalsedomain\userNT Authority\Network Service

Legend:

  • Where domain\user will show up as:
    • domain\user for Active Directory
    • machineName\userName for local account
  • Where NT Authority\Network Service will show up as:
    • NT Authority\Network Service for Windows Server or ASP.NET
    • machineName\ASPNET_WP for Windows XP

Resource

https://stackoverflow.com/questions/5402249/httpcontext-current-user-principal-vs-windowsidentity-getcurrent