PrepAway - Latest Free Exam Questions & Answers

How should you complete the code?

DRAG DROP
You have the following code.

You need to return all of the products and their associated categories.
How should you complete the code? To answer, drag the appropriate code elements to the correct targets in
the answer area. Each code element may be used once, more than once, or not at all. You may need to drag
the split bar between panes or scroll to view content.
Select and Place:

PrepAway - Latest Free Exam Questions & Answers

Answer:

Explanation:
Example: Join operations create associations between sequences that are not explicitly modeled in the data
sources. For example you can perform a join to find all the customers and distributors who have the same
location. In LINQ the join clause always works against object collections instead of database tables directly.
C#
var innerJoinQuery =
from cust in customers
join dist in distributors on cust.City equals dist.City
select new { CustomerName = cust.Name, DistributorName = dist.Name };
https://msdn.microsoft.com/en-us/library/bb397927.aspx
https://msdn.microsoft.com/en-us/library/bb397927.aspx

2 Comments on “How should you complete the code?

  1. cristis says:

    public class Product
    {
    public string Name { get; set; }

    public int CategoryId { get; set; }
    }

    public class Category
    {
    public int Id { get; set; }

    public string Name { get; set; }
    }

    class Program
    {
    static void Main(string[] args)
    {
    var categories = new List()
    {
    new Category { Id = 1, Name = “Food”},
    new Category { Id = 2, Name = “Clothing”},
    };

    var products = new List()
    {
    new Product { Name = “Strawberry”, CategoryId = 1},
    new Product { Name = “Banana”, CategoryId = 1},
    new Product { Name = “Pants”, CategoryId = 2},
    };

    var productsWithCategories = from product in products
    join category in categories
    on product.CategoryId equals category.Id
    select new
    {
    Naame = product.Name,
    Category= category.Name
    };

    foreach (var item in productsWithCategories)
    {
    Console.WriteLine($”{item.Naame} {item.Category}”);
    }

    Console.ReadKey();
    }
    }

    Displays:
    Strawberry Food
    Banana Food
    Pants Clothing




    0



    0

Leave a Reply