where should i use method based linq and query based linq RRS feed

  • Question

  • User2102072086 posted


    I have seen people giving preference to method based <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="5" data-gr-id="5">linq</g> ( in c#), so <g class="gr_ gr_7 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="7" data-gr-id="7">i</g> wanted to know where one should use a method based on <g class="gr_ gr_4 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="4" data-gr-id="4">linq</g> and where one should use query-based <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="6" data-gr-id="6">linq</g>?

    yours sincerely

    Wednesday, October 24, 2018 4:35 AM


All replies

  • User1120430333 posted

    Method based is going to be used on something that is using IEnumerable<T> most of the time,  and you should be able to IntelliSense  when you can use a Linq method. The method() can use lambada. 


    To be honest, Resharper taught me how to use Linq. :) It would evaluate code and suggest a change, and 9 times out of 10, I would just go with the suggested change. That lead to me knowing when I could use Linq in various ways and made me curious, experiment and Resharper would make the corrections, when I would use Google and look up a Linq method about just what it is and how can I use it.

    You can do something like this too.

    var list = GetSomeList().Where( a => a.Name.contains ("rajemessage").ToList().

     if the method() is returning a collection, then you can apply Linq to the collection that is returned out of the method(). 

    public ProjectViewModels GetProjectsByUserId(string userid)
                var vm = new ProjectViewModels {Projects = new List<ProjectViewModels.Project>()};
                var dtos = _webApi.GetProjsByUserIdApi(userid).ToList();
                vm.Projects.AddRange(dtos.Select(dto => new ProjectViewModels.Project()
                    ProjectId = dto.ProjectId,
                    ClientName = dto.ClientName,
                    ProjectName = dto.ProjectName,
                    Technology = dto.Technology,
                    ProjectType = dto.ProjectType,
                    StartDate = dto.StartDate,
                    EndDate = dto.EndDate,
                    Cost = dto.Cost
                return vm;
     public DtoTask GetTaskById(int id)
                var dto = new DtoTask();
                using (var context = new ProjectManagementContext(_options))
                    var task = (context.Tasks.Where(a => a.TaskId == id)).SingleOrDefault();
                    if (task == null) return dto;
                    dto.TaskId = task.TaskId;
                    dto.TaskName = task.TaskName;
                    dto.Note = task.Note;
                    dto.StartDate = task.StartDate;
                    dto.EndDate = task.EndDate;
                    dto.ResourceId = task.ResourceId;
                    dto.ProjectId = (int)task.ProjectId;
                    dto.TaskDuration = task.TaskDuration;
                    dto.TaskSpent = task.TaskSpent;
                    dto.Status = task.Status;
                return dto;

    Wednesday, October 24, 2018 7:29 AM
  • User-821857111 posted

    It's purely personal preference. If you are more comfortable with SQL, you might prefer query syntax because it is quite similar. My preference happens to be for method syntax. You can do the same thing with either approach, so it's not like one approach offers features that the other doesn't.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 24, 2018 7:54 AM
  • User1520731567 posted

    Hi rajemessage,

    I have seen people giving preference to method based linq ( in c#), so i wanted to know where one should use a method based on linq and where one should use query-based linq?

    There is no clear answer to this question.

    LINQ comes in two syntactical flavors: The Query and the Method syntax.

    They can do almost the same, but while the query syntax is almost a new language within C#, the method syntax looks just like regular C# method calls.

    From this article:

    • Query syntax is easier to read but Method syntax are more powerful way to writing queries.
    • Both syntax's does not support all query operators. Some operators are not supported by Query Syntax and some are not by Method Syntax.
    • Always prefer Query syntax over Method syntax until requirements demand.

    Different people have different opinions.

    Best Regards.

    Yuki Tao

    Thursday, October 25, 2018 7:34 AM