【问题标题】:Selecting multiple columns with linq query and lambda expression使用 linq 查询和 lambda 表达式选择多列
【发布时间】:2014-10-03 13:07:16
【问题描述】:

我是 C# ASP.NET 的新手,正在开发我的第一个应用程序。

我正在尝试创建一个返回数组的 linq 语句。

我有一张产品表。我希望能够为状态 == 1 的每个产品选择名称、ID 和价格。

我正在努力寻找一种方法来做到这一点。 我只能返回单个项目/列。我一直坚持这种方式很长时间。

这是我目前所拥有的:

try
{
  using (UserDataDataContext db = new UserDataDataContext())
  {
    return db.mrobProducts.Select(x => x.Name).OrderBy(x => x).ToArray();
  }
}

如果您查看下面的屏幕截图,您会看到我有 2 个错误, Select = Type 对象不能从它的用法中被引用 ToArray = 无法将符号解析为数组

【问题讨论】:

  • 我无法在其中添加 where 子句,无论我在何处或如何添加它都会产生错误,
  • return a 2 dimensional arrary??
  • @GrantWinney,我已经更新了问题,因为我一直在执行每个人的答案,但这里仍然存在问题,,,
  • string[] 更改为 Tuple<int, string, string>

标签: c# sql asp.net linq


【解决方案1】:

不确定您的表结构是什么样的,但请参见下文。

public NamePriceModel[] AllProducts()
{
    try
    {
        using (UserDataDataContext db = new UserDataDataContext())
        {
            return db.mrobProducts
                .Where(x => x.Status == 1)
                .Select(x => new NamePriceModel { 
                    Name = x.Name, 
                    Id = x.Id, 
                    Price = x.Price
                })
                .OrderBy(x => x.Id)
                .ToArray();
         }
     }
     catch
     {
         return null;
     }
 }

这将返回一个匿名类型的数组,其中包含您需要的成员。

更新:

创建一个新类。

public class NamePriceModel 
{
    public string Name {get; set;}
    public decimal? Price {get; set;}
    public int Id {get; set;}
}

我已经修改了上面的查询以返回它,你应该将你的方法从返回 string[] 更改为返回 NamePriceModel[]

【讨论】:

  • 返回get是一个错误,说:Cant convert expression type Namstring, Pricestring to return type string[]
  • @Mark 我不确定您是否有适合您正在寻找的解决方案的返回类型。如果您对为其创建类型不感兴趣,您可以考虑返回动态。不过,类型会提供更多的清晰度。
  • @scaretag,你能根据我更新的问题提供一个例子吗?
  • @Mark 我已经做到了。我已经用一个例子更新了我的答案。
  • OrderBy(x => x) 将不起作用,如果该类未在 NamePriceModel 类中实现 IComparable<T> 接口。或者 OP 必须按某个列排序。
【解决方案2】:

你可以使用:

public YourClass[] AllProducts()
{
    try
    {
        using (UserDataDataContext db = new UserDataDataContext())
        {
            return db.mrobProducts.Where(x => x.Status == 1)
                           .OrderBy(x => x.ID)
                           .Select(x => new YourClass { ID = x.ID, Name = x.Name, Price = x.Price})
                           .ToArray();
        }
    }
    catch
    {
        return null;
    }
}

这里是YourClass 实现:

public class YourClass
{
  public string Name {get; set;}
  public int ID {get; set;}
  public int Price {get; set;}
}

并且您的AllProducts 方法的返回类型必须是YourClass[]

【讨论】:

  • 感谢您的回答。为我工作。优化查询。
【解决方案3】:

使用 LINQ 和 Lamba,我想返回两个字段值并将其分配给单个实体对象字段;

作为名称 = Fname + " " + LName;

查看我下面的代码,它按预期工作;希望这有用;

Myentity objMyEntity = new Myentity
{
id = obj.Id,
Name = contxt.Vendors.Where(v => v.PQS_ID == obj.Id).Select(v=> new { contact = v.Fname + " " + v.LName}).Single().contact
}

无需声明“联系人”

【讨论】:

    【解决方案4】:
            Object AccountObject = _dbContext.Accounts
                                       .Join(_dbContext.Users, acc => acc.AccountId, usr => usr.AccountId, (acc, usr) => new { acc, usr })
                                       .Where(x => x.usr.EmailAddress == key1)
                                       .Where(x => x.usr.Hash == key2)
                                       .Select(x => new { AccountId = x.acc.AccountId, Name = x.acc.Name })
                                       .SingleOrDefault();
    

    【讨论】:

    • 能否请您详细说明您提出的代码?
    • 这里 Accounts 和 User 表是通过 AccountId 外键连接的。 where 条件应用于用户表列。我们从 Accounts Table 中选择两列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多