-1
public ClientApp_Detail GetHomeDetbyClientID_AppID(string clientid, long appId)
{
    ClientApp_Detail sp = null;

    using (HelpDeskDataContext HDDC = Conn.GetContext())
    {

        sp = (from st in HDDC.ClientApp_Details where  st.ClientID == clientid && st.AppID == appId && st.IsDeleted != true select st).FirstOrDefault();

    }

    return sp;
}

As I am a beginner to .NET, I am unable to understand the use of the above query and why it is declared as from st in. Please explain the usage of such type query and what is returning to sp(example) variable.

psubsee2003
  • 8,563
  • 8
  • 61
  • 79

2 Answers2

2
from st in HDDC.ClientApp_Details

is something like

foreach(var st in HDDC.ClientApp_Details)

Than it filters by following critiria

st.ClientID == clientid && st.AppID == appId && st.IsDeleted != true select st

And last

FirstOrDefault();

will return first elemant or default value (default(T)) if nothing matched.

P.S. As it was already told take a look into LINQ tutorials. It's very simple. Or you can download LinqPad. LINQ sampels are included and probably it's the best way to play with LINQ.

Artiom
  • 7,694
  • 3
  • 38
  • 45
  • 2
    _"`from st in` can be translated to `foreach`"_ - no, it can't. I understand it's a simplification, but it's not exactly correct. Linq is a C# language construct that gets translated to lazily-evaluated `System.Linq` statements. – CodeCaster Jan 07 '15 at 09:12
  • 2
    @CodeCaster I meant it can be read (for user) as it. Thank you for the note. – Artiom Jan 07 '15 at 09:14
-1

Here HelpDeskDataContext is a entity framework context.

(from st in HDDC.ClientApp_Details where  st.ClientID == clientid && st.AppID == appId && st.IsDeleted != true select st).FirstOrDefault();

The above statement is a LINQ query where ClientApp_Details is an Entity of the HelpDeskDataContext.

It is like a sql query---

select * from ClientApp_Details where st.ClientID==clientid && st.AppID == appId && st.IsDeleted != true

FirstOrDefault() has been used if the does not match the criteria then it will return a default value.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
Kaptan
  • 336
  • 2
  • 11