0

我有一个方法,用于从我的表中获取选定行的名为 ItemNumber、ItemDescription、UnitDescription 的 3 列的值。我已经传递了该选定行的参数 ItemId,并且必须获取不同变量中的每一列值。我不'不知道如何处理它。谁能告诉我如何做到这一点。这是我的代码-

  public System.Collections.IEnumerable FetchDataItem(int id)
    {
        var row = (from t1 in context.InventoryUnit
                   join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId
                   where t2.ItemId == id
                   orderby t2.ItemNumber, t2.ItemDescription
                   select new
                  {

                   t2.ItemNumber,
                   t2.ItemDescription,
                    t1.UnitDescription
                  });


        return row.ToList();
    }
4

1 回答 1

0

Satpal 给出了一个很好的例子,但是如果您不想创建一个类,您可以使用它并将其选择为匿名类型。

public static void Main()
{
    // Short example...
    var rows = (from t1 in context.InventoryUnit
                join t2 in context.InventoryItem on t1.UnitId equals t2.UnitId
                where t2.ItemId == id
                orderby t2.ItemNumber, t2.ItemDescription
                select new
                {
                    ItemNumber = t2.ItemNumber,
                    ItemDescription = t2.ItemDescription,
                    UnitDescription = t1.UnitDescription
                 });

    // Here you still have access to data and the types of the data.

    foreach (var row in row)
    {
        // do something with the data....
        var someint = row.ItemNumber;
        var someString = row.ItemDescription;
        var someotherstring = row.ItemDescription;
    }
}
于 2013-11-09T14:48:02.637 回答