Home

How can I use Distinct (C# statement)

edited November 2017
var query = from log in ViewLogs
join v in Vehicles on log.VehicleId equals v.VehilceId
join d in Sellers on v.SellerId equals d.SellerId
where v.IsActive && !v.IsDeleted && d.IsActive && d.IsPublic
select log.VehicleId;

query.Dump();
I want the Distinct VehicleId to be listed. VehicleId is Guid. How to do this?

Also is there an option that I run a SQL query that generates the LINQ, i.e. the other way round?

Thanks!

Comments

  • edited November 2017
    How about:

    query.Distinct().Dump();

    Note that you would be better off using association properties.
    var query = from log in ViewLogs
       let v = log.Vehicle
       let s = v.Seller
       where v.IsActive && v.IsDeleted && s.IsActive && s.IsPublic
       select log.VehicleId;
    
    query.Distinct().Dump();
    You're in for a lot of unnecessary pain if you simply transliterate SQL into LINQ. For more info, read this:

    https://www.linqpad.net/WhyLINQBeatsSQL.aspx
Sign In or Register to comment.