Cannot implicitly convert type 'System.Linq.IQueryable' to 'int?'

前端 未结 6 1800
梦毁少年i
梦毁少年i 2021-02-06 23:14
var cityList = from country in 
                    doc.Element(\"result\")
                    .Element(\"cities\")
                    .Descendants(\"city\")
select ne         


        
6条回答
  •  日久生厌
    2021-02-06 23:26

    It has elapsed a long time since the last update to the post but i think it's worth improving the solution.

    In my opinion the solutions posted for this particular scenario are not the best way in terms of performace to get the ID you need. A better solution is as follows.

    db.Countries.Where(a=>a.DOTWInternalID == citee.CountryCode)
                .Select(a => a.ID).FirstOrDefault();
    

    The previous statemants basically runs a SQL query similar to the following one:

    SELECT TOP (1) ID
    FROM [dbo].[Countries]
    WHERE DOTWInternalID = 123
    

    The proposed solutions work but basically do a "SELECT *" to create the entity with all the values and then obtain the ID from the object just created.

    You can use Linqpad to actually see the generated SQL and tune up LINQ queries or Lambdas.

    Hope it helps to some others that get to this post.

提交回复
热议问题