代码之家  ›  专栏  ›  技术社区  ›  RameshVel

将此LINQ表达式转换为Lambda

  •  14
  • RameshVel  · 技术社区  · 15 年前

    var result = from g in grocery
           join f in fruit on g.fruitId equals f.fruitId into tempFruit
           join v in veggie on g.vegid equals v.vegid into tempVegg
           from joinedFruit in tempFruit.DefaultIfEmpty()
           from joinedVegg in tempVegg.DefaultIfEmpty()
           select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty :     joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };
    

    谁能建议我怎么做。

    我真的很感激如果有人给我很好的教程链接 “C#Lambdas&Linqs”

    7 回复  |  直到 15 年前
        1
  •  29
  •   Brad Parks    11 年前

    要将Linq查询转换为其Lambda等价项,请执行以下操作:

    1. 下载 Linqpad 跑你的车 查询
    2. 在结果窗口中,单击 在工具栏的“”按钮上。 就在结果窗口的正上方
    3. 您的查询将转换为 Lambda表达式等价物!

    enter image description here

        3
  •  11
  •   Jim G.    15 年前

    以下是我遵循的启发:

    当有连接时,喜欢LINQ表达式而不是lambdas。

        4
  •  10
  •   Mark Coleman    15 年前

        var result = from g in grocery
                     join f in fruit on g.fruitId equals f.fruitId into tempFruit
                     join v in veggie on g.vegid equals v.vegid into tempVegg
                     from joinedFruit in tempFruit.DefaultIfEmpty()
                     from joinedVegg in tempVegg.DefaultIfEmpty()
                     select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };
    

    然后使用ReSharper的convert LINQ to method chain选项,等于以下值:

            var result =grocery .GroupJoin(fruit, g => g.fruitId, f => f.fruitId, (g, tempFruit) => new {g, tempFruit})
                                .GroupJoin(veggie, @t => @t.g.vegid, v => v.vegid, (@t, tempVegg) => new {@t, tempVegg})
                                .SelectMany(@t => @t.@t.tempFruit.DefaultIfEmpty(), (@t, joinedFruit) => new {@t, joinedFruit})
                                .SelectMany(@t => @t.@t.tempVegg.DefaultIfEmpty(),(@t, joinedVegg) =>
                                    new
                                        {
                                            @t.@t.@t.g.fruitId,
                                            @t.@t.@t.g.vegid,
                                            fname = ((@t.joinedFruit == null) ? string.Empty : @t.joinedFruit.fname),
                                            vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname)
                                        });
    

        5
  •  4
  •   menjaraz    12 年前

    以下是如何使用lambda编写此查询:

    var cus­tomers = new List {
    new Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1” },
    new Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2” },
    };
    
    var user­Cus­tomers = new List {
    new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1”, User = “not-admin” },
    new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1”, User = “admin” },
    new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer2”, User = “not-admin” },
    new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer2”, User = “admin” },
    new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer1”, User = “not-admin”     },
    new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer1”, User = “admin” },
    new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2”, User = “not-admin” },
    new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2”, User = “admin” }
    };
    

    使用查询表达式

    var query =
    from c in cus­tomers
    join uc in user­Cus­tomers on
    new { c.CompanyId, c.CustomerId } equals new { uc.CompanyId, uc.CustomerId }
    where c.CompanyId == “AC” && uc.User == “admin“
    select c;
    

    使用lambda表达式

    var lambda =  cus­tomers.Where(c => c.CompanyId == “AC”) // inner sequence
    .Join(userCustomers.Where(uc => uc.User == “admin”), // outer sequence
    c => new { c.CompanyId, c.CustomerId }, // inner key selec­tor
    uc => new { uc.CompanyId, uc.CustomerId }, // outer key selec­tor
    (c, uc) => c);
    

    这两种方法都会产生相同的结果(公司Id为AC的客户和客户Id为Customer1的客户),但正如您所看到的,lambda表达式更难编写和读取!

    希望这有帮助!

        6
  •  3
  •   Mitch Wheat    15 年前

    LINQPad ; 它附带用于学习LINQ的内置示例。

        7
  •  3
  •   leppie    15 年前