英文:
How to intersect two select with int and string type in LINQ Method syntax?
问题
我是新手程序员,希望你能帮我写出更好的代码。
**描述**
我想编写一个查询,打印出加利福尼亚州活跃且名字中包含字母R或D的人的平均分数。
**示例输入**
StudentName = {"Ben Wild", Score = 65, StudentCity = "CA", StudentActvity= true}
StudentName = {"Sara Ride", Score = 81, StudentCity = "CA", StudentActvity = true}
**我的问题**
我需要选择他们的姓名和分数,但我不能选择两个Lambda表达式并使用`&&`连接它们。
我得到了这个错误:`'string'不包含'Score'的定义,也没有可访问的扩展方法'Score'来接受类型为'string'的第一个参数`
我尝试了很多方法,我将在下面提到我的解决方案:
**Program.cs 代码**
using System;
using System.Collections.Generic;
using System.Linq;
namespace _64_LINQ_Practice
{
class Program
{
static void Main(string[] args)
{
//访问类方法
var studentList = StudentDatabase.GetStudentsFromDb();
var studentsName6 = studentList.Where(x => (x.StudentActvity == true) && (x.StudentCity == "CA")).Select(x => x.StudentName);
List
IEnumerable
x => x.Contains("R") || x.Contains("r") || x.Contains("d") || x.Contains("D"));
foreach (string studentFinal in final)
{
Console.WriteLine("活跃,住在CA并且名字中包含r/R或D/d字母的学生:" + studentFinal);
}
}
}
}
**第二种方式:**
var studentsName6 = studentList.Where(x => (x.StudentActvity == true) && (x.StudentCity == "CA")).Average(x => x.Score);
List<string> xstudent = new List<string>(studentList.Select(x => x.StudentName));
List<int> intstudent = new List<int>(studentList.Select(x => x.Score));
IEnumerable<string> final = xstudent.Where(
x => x.Contains("R") || x.Contains("r") || x.Contains("d") || x.Contains("D"));
IEnumerable<int> final2 = intstudent;
var convFinal = final.AsQueryable();
var convFinal2 = final2.AsQueryable();
var final3 = studentsName6.AsQueryable().Concat(convFinal.AsQueryable()).Concat(convFinal2);
var final4 = final3.Average(x => x.Score);
Console.WriteLine(final4);
英文:
I am new to programming I hope you can help me to write better code.
Description
I want to write a query that prints Average scores of Californians who are active and have the letter R or D in their name.
Example Input
StudentName = {"Ben Wild", Score = 65, StudentCity = "CA", StudentActvity= true}
StudentName = {"Sara Ride", Score = 81, StudentCity = "CA", StudentActvity = true}
My problem
I need to select their name and their score at the end but I can't select two Lambda expressions and &&
them together.
I got this error : 'string' does not contain a definition for 'Score' and no accessible extension method 'Score' accepting a first argument of type 'string' could be
I tried so many ways that I will mention my solutions below :
Program.cs code
using System;
using System.Collections.Generic;
using System.Linq;
namespace _64_LINQ_Practice
{
class Program
{
static void Main(string[] args)
{
//Access to the class method
var studentList = StudentDatabase.GetStudentsFromDb();
var studentsName6 = studentList.Where(x => (x.StudentActvity == true) && (x.StudentCity == "CA")).Select(x => x.StudentName);
List<string> xstudent = new List<string>(studentList.Select(x => x.StudentName));
IEnumerable<string> final = xstudent.Where(
x => x.Contains("R") || x.Contains("r") || x.Contains("d") || x.Contains("D"));
foreach (string studentFinal in final)
{
Console.WriteLine("The students that are active, live in CA and names with r/R or D/d letter :" + studentFinal);
}
}
}
}
and second way:
var studentsName6 = studentList.Where(x => (x.StudentActvity == true) && (x.StudentCity == "CA")).Average(x => x.Score);
List<string> xstudent = new List<string>(studentList.Select(x => x.StudentName));
List<int> intstudent = new List<int>(studentList.Select(x => x.Score));
IEnumerable<string> final = xstudent.Where(
x => x.Contains("R") || x.Contains("r") || x.Contains("d") || x.Contains("D"));
IEnumerable<int> final2 = intstudent;
var convFinal = final.AsQueryable();
var convFinal2 = final2.AsQueryable();
var final3 = studentsName6.AsQueryable().Concat(convFinal.AsQueryable()).Concat(convFinal2);
var final4 = final3.average(x => x.Score);
Console.WriteLine(final4);
答案1
得分: 0
以下是翻译好的内容:
"代码中没有尝试计算平均值,所以不清楚问题出在哪里。根据以下条件,输出学生的平均分数:住在加利福尼亚(CA)且活动为真,名字中包含 R/r 或 D/d 字母的学生的平均分数。
似乎只需要一个 Where
条件来筛选学生,然后使用 Avg
计算平均值。由于不需要计算多个分组的平均值,不需要使用 GroupBy
。"
var letters = new[] { "r", "d" };
var average = studentList.Where(st =>
st.StudentActvity
&& st.StudentCity == "CA"
&& letters.Any(l => st.StudentName.Contains(l, StringComparison.OrdinalIgnoreCase))
)
.Average(st => st.Score);
String.Contains 可以接受一个参数,指定不区分大小写的比较,因此不需要同时指定大小写字母。
letters.Any(l => st.StudentName.Contains(l...))
检查是否在 StudentName
中包含任何字母。
这个查询将适用于内存数据库和可能是实际数据库。不过,它将非常慢。name.Contains("r")
被转换成 SQL 条件 name LIKE '%r%'
。无法使用索引快速查找具有 r
的名字,因此数据库将不得不搜索整个表。
英文:
There's no attempt to calculate averages in the code so it's unclear what the problem is. Going by
> prints the average score of students, with these conditions: Average score of students that live in CA and their activity is true and there are R/r or D/d letters contain in their name.
It seems a single Where
is needed to filter the students, then Avg
to calculate the average. Since there's no need to calculate averages for multiple groups, GroupBy
isn't needed.
var letters=new[]{"r","d"};
var average=studentList.Where(st=>
st.StudentActvity
&& st.StudentCity == "CA"
&& letters.Any(l=>st.StudentName.Contains(l,StringComparison.OrdinalIgnoreCase))
)
.Average(st=>st.Score);
String.Contains can accept a parameter that specifies case-insensitive comparison, so there's no need to specify both small and capital letters.
letters.Any(l=>st.StudentName.Contains(l...))
checks if any letter is contained inside StudentName
This query will work with an in-memory database and probably an actual database. It will be very slow though. name.Contains("r")
is translated to the SQL condition name LIKE '%r%'
. There's no way to use an index to quickly find which names have r
in their middle, so the database will have to search the entire table
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论