使用一个数组中的所有值创建一个数组,这些值与 C# 中另一个数组中的值匹配
Make an array with all the values from one array which match the value in another array in C#
我有一个这样的数据库class:
public partial class Colour
{
public int ColourID { get; set; }
public string Colour1 { get; set; }
}
另一个 class 这样的:
public partial class RangeColour
{
public int RangeColourID { get; set; }
public Nullable<int> RangeID { get; set; }
public Nullable<int> ColourID { get; set; }
}
第三个 class 像这样:
public partial class Range
{
public int RangeID { get; set; }
public string RangeName { get; set; }
}
每个 Range
实例可以有来自 Colour
class 的多种颜色。所有范围和它们的颜色都链接在 RangeColour
class 中。
这种关系建立如下:Range
中的RangeID
table是RangeColour
中的RangeID
的主键table.
类似地,Colour
ColourID
table 是 RangeColour
table 中 ColourID
的主键。
我在我的 c# 方法中得到一个特定范围的 IEnumerable
,如下所示:
public class HomeController : Controller
{
Entities db = new Entities();
public ActionResult Product(int id)
{
IEnumerable<RangeColour> colran = db.RangeColours.Where(c => c.RangeID == id);
//other code
}
}
现在我已经得到属于 RangeID
的所有 colourID
的 RangeColours
table 的 IEnumerable
我该如何制作新的 IEnumerable
包含所有 Colour1
和 ColourID
中的 Colour
table 的值?我确信这可以通过某种 lambda 表达式来完成。
您可以在 IQueryable 上使用 Join
扩展来获取颜色。
IQueryable<Colour> colors = db.Colors.Join(
db.RangeColours.Where(c => c.RangeID == item.RangeID),
c => c.ColourID,
r => r.ColourID,
(c, r) => c);
然后,如果需要,您可以 select 您的 Colour1 字符串。并在内存中需要时使用 ToList()
。
colors.Select(c => c.Colour1).ToList()
我有一个这样的数据库class:
public partial class Colour
{
public int ColourID { get; set; }
public string Colour1 { get; set; }
}
另一个 class 这样的:
public partial class RangeColour
{
public int RangeColourID { get; set; }
public Nullable<int> RangeID { get; set; }
public Nullable<int> ColourID { get; set; }
}
第三个 class 像这样:
public partial class Range
{
public int RangeID { get; set; }
public string RangeName { get; set; }
}
每个 Range
实例可以有来自 Colour
class 的多种颜色。所有范围和它们的颜色都链接在 RangeColour
class 中。
这种关系建立如下:Range
中的RangeID
table是RangeColour
中的RangeID
的主键table.
类似地,Colour
ColourID
table 是 RangeColour
table 中 ColourID
的主键。
我在我的 c# 方法中得到一个特定范围的 IEnumerable
,如下所示:
public class HomeController : Controller
{
Entities db = new Entities();
public ActionResult Product(int id)
{
IEnumerable<RangeColour> colran = db.RangeColours.Where(c => c.RangeID == id);
//other code
}
}
现在我已经得到属于 RangeID
的所有 colourID
的 RangeColours
table 的 IEnumerable
我该如何制作新的 IEnumerable
包含所有 Colour1
和 ColourID
中的 Colour
table 的值?我确信这可以通过某种 lambda 表达式来完成。
您可以在 IQueryable 上使用 Join
扩展来获取颜色。
IQueryable<Colour> colors = db.Colors.Join(
db.RangeColours.Where(c => c.RangeID == item.RangeID),
c => c.ColourID,
r => r.ColourID,
(c, r) => c);
然后,如果需要,您可以 select 您的 Colour1 字符串。并在内存中需要时使用 ToList()
。
colors.Select(c => c.Colour1).ToList()